Tk grid will not change

I am trying to write a simple ui with Tkinter in python, and I cannot force the widgets in the grid to resize. Whenever I resize the main window, login widgets and buttons are not configured at all.

Here is my code:

class Application(Frame): def __init__(self, master=None): Frame.__init__(self, master, padding=(3,3,12,12)) self.grid(sticky=N+W+E+S) self.createWidgets() def createWidgets(self): self.dataFileName = StringVar() self.fileEntry = Entry(self, textvariable=self.dataFileName) self.fileEntry.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W) self.loadFileButton = Button(self, text="Load Data", command=self.loadDataClicked) self.loadFileButton.grid(row=0, column=3, sticky=N+S+E+W) self.columnconfigure(0, weight=1) self.columnconfigure(1, weight=1) self.columnconfigure(2, weight=1) app = Application() app.master.title("Sample Application") app.mainloop() 
+6
source share
3 answers

Add a root window and columnconfigure so that your Frame widget also expands. In this problem, you have an implicit root window, if you do not specify one, and the frame itself is something that does not spread properly.

 root = Tk() root.columnconfigure(0, weight=1) app = Application(root) 
+14
source

I use the package for this. In most cases, this is enough. But do not mix both!

 class Application(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.pack(fill = X, expand =True) self.createWidgets() def createWidgets(self): self.dataFileName = StringVar() self.fileEntry = Entry(self, textvariable=self.dataFileName) self.fileEntry.pack(fill = X, expand = True) self.loadFileButton = Button(self, text="Load Data", ) self.loadFileButton.pack(fill=X, expand = True) 
0
source

Working example. Note that you need to explicitly configure configure for each column and row used, but the columns for the button below are more than the number of columns displayed.

 ## row and column expand top=tk.Tk() top.rowconfigure(0, weight=1) for col in range(5): top.columnconfigure(col, weight=1) tk.Label(top, text=str(col)).grid(row=0, column=col, sticky="nsew") ## only expands the columns from columnconfigure from above top.rowconfigure(1, weight=1) tk.Button(top, text="button").grid(row=1, column=0, columnspan=10, sticky="nsew") top.mainloop() 
0
source

Source: https://habr.com/ru/post/914979/


All Articles