Home > Mobile >  How do I make my widgets in tkinter adjust their size automatically with respect to the window size?
How do I make my widgets in tkinter adjust their size automatically with respect to the window size?

Time:10-02

My tkinter window has about 16 buttons all placed equidistant from one another in a square. I used ttk to create these objects and used place() to place them on the window. When the user resizes a window by dragging the corner of the window, I want the buttons and the text in the buttons to automatically grow/shrink based on whether the user increases the size or decreases the window size.

CodePudding user response:

To grow widgets as their size increase I use weight option along with sticky for grid manager. Since you said you are having a square, I have made 16 buttons in 4 rows and 4 columns. So this is what it looks like:

from tkinter import *

root = Tk()

for i in range(4):
    root.grid_rowconfigure(i,weight=1) # i is the row index
    for j in range(4):
        Button(root,text=f'{4*i j}th Button', width=10).grid(row=i,column=j,sticky='news')
        root.grid_columnconfigure(j,weight=1) # j is column index

root.mainloop()

So sticky='news' will make the widget take the entire space on the cell. And weight will provide the additional space to propagate for the given row/column.

  • Related