Python 3.6 - Resize Tkinter buttons to fit border
I've been looking back on stack overflow for years now trying to find an answer to this question, but I just can't get anything to work, so I ask this question. I have a small program with three buttons and a label, and they are in a grid. I was wondering how regardless of size or shape, the button and label will stay the same relative to the border. Likewise, if I resize the image, everything stays the same size.
Here is my code:
from tkinter import *
class Window(Frame): #All the stuff for the GUI
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
self.grid()
def init_window(self):
self.master.title("EncryptDecrypt")
self.pack(fill = BOTH, expand = 1)
quitButton = Button(self, text = "Quit", command = self.client_exit, width = 10, height = 5) #Quit Button
quitButton.grid(row = 0, column = 0, sticky = W)
encryptModeButton = Button(self, text = "Encrypt", command = lambda: self.execute("decrypted.txt", "encrypted.txt", 1, 0), width = 10, height = 5) #Encrypt Button
encryptModeButton.grid(row = 0, column = 1, sticky = W)
decryptModeButton = Button(self, text = "Decrypt", command = lambda: self.execute("encrypted.txt", "decrypted.txt", 0, 1), width = 10, height = 5) #Decrypt button
decryptModeButton.grid(row = 0, column = 2, sticky = W)
myLabel = Label(self, text = "Select The Action You Wish To Undertake", font = ("Purisa", 15))
myLabel.grid(row = 0, column = 3)
root = Tk()
root.geometry("610x80")
app = Window(root)
root.mainloop()
Sorry if the answer is obvious, I've already tried pack()
source to share
There is a good tutorial for a mesh wrapper in there . Just scroll to Handle Resize and you will notice how to use the option sticky
and customize the weight
column / row pairs.
So try your example with a packer grid
:
from tkinter import *
class Window(Frame): #All the stuff for the GUI
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.master.minsize(width=650, height=80)
self.configure(relief=RAISED, borderwidth=10)
self.init_window()
self.grid(sticky = NSEW)
def init_window(self):
self.master.title("EncryptDecrypt")
# configure weights; note: that action need for each container!
self.master.rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
for i in range(4):
self.columnconfigure(i, weight=1)
quitButton = Button(self, text = "Quit", width = 10, height = 5) #Quit Button
quitButton.grid(row = 0, column = 0, sticky = NSEW)
encryptModeButton = Button(self, text = "Encrypt", width = 10, height = 5) #Encrypt Button
encryptModeButton.grid(row = 0, column = 1, sticky = NSEW)
decryptModeButton = Button(self, text = "Decrypt", width = 10, height = 5) #Decrypt button
decryptModeButton.grid(row = 0, column = 2, sticky = NSEW)
myLabel = Label(self, text = "Select The Action You Wish To Undertake", font = ("Purisa", 15))
myLabel.grid(row = 0, column = 3, sticky = NSEW)
root = Tk()
root.geometry("650x80")
app = Window(root)
root.mainloop()
As you can see - I just added sticky=NSEW
and columnconfigure
/ rowconfigure
and it seems to work as you wish! The downside is the need to customize each container!
But here, in the manager pack
, there are more intuitive roles that fulfill the same parameters - fill
and expand
!
from tkinter import *
class Window(Frame): #All the stuff for the GUI
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.master.minsize(width=650, height=80)
self.configure(relief=RAISED, borderwidth=10)
self.init_window()
self.pack(fill=BOTH, expand=True)
def init_window(self):
self.master.title("EncryptDecrypt")
quitButton = Button(self, text = "Quit", width = 10, height = 5) #Quit Button
quitButton.pack(fill=BOTH, side=LEFT, expand=True)
encryptModeButton = Button(self, text = "Encrypt", width = 10, height = 5) #Encrypt Button
encryptModeButton.pack(fill=BOTH, side=LEFT, expand=True)
decryptModeButton = Button(self, text = "Decrypt", width = 10, height = 5) #Decrypt button
decryptModeButton.pack(fill=BOTH, side=LEFT, expand=True)
myLabel = Label(self, text = "Select The Action You Wish To Undertake", font = ("Purisa", 15))
myLabel.pack(fill=BOTH, side=LEFT, expand=True)
root = Tk()
root.geometry("650x80")
app = Window(root)
root.mainloop()
What to use is your choice! And there is a nice topic about resizing, gridding and packing! Take a look
Some other useful links:
source to share