Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter: responsive grid

I have a frame with multiple child elements, that are placed in it using the grid() geometry manager.

How can I modify the code below to make the frame responsive?

content.grid(column=0, row=0, sticky='nwse')
userButt.grid(column=2, row=1, sticky='nwse')
favoButt.grid(column=3, row=1, sticky='nwse')
locaButt.grid(column=4, row=1, sticky='nwse')
histScal.grid(column=5, row=1, sticky='nwse')
like image 843
Kevin Jordil Avatar asked Oct 27 '25 21:10

Kevin Jordil


2 Answers

As a rule of thumb, whenever you use grid you should always give at least one row and one column a non-zero weight so that tkinter knows where to allocate extra space. A weight of 0 (zero) is assigned by default.

The two most common cases are where you have a "hero" widget (eg: a text widget, canvas widget, etc) that should grow and shrink as necessary, or you want everything to resize equally. For the case where one widget gets all the extra space, give a weight just to the row and column where that widget is placed. If you want everything to resize equally, give each row and each column a weight.

Assuming that the parent of your widgets content, userButt, etc are root, you might do it like this:

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

In the above example, all extra space would go to row zero and column 0.

like image 88
Bryan Oakley Avatar answered Oct 30 '25 12:10

Bryan Oakley


Suppose you have a window that is managed by grid system and you want to make it responsive knowing the total number of rows and column you used. Let's say the total number of rows =6 and the total number of columns =10 making the window responsive under grid management system can be done as follows.

n_rows =6
n_columns =10
for i in range(n_rows):
    root.grid_rowconfigure(i,  weight =1)
for i in range(n_columns):
    root.grid_columnconfigure(i,  weight =1)
like image 28
crispengari Avatar answered Oct 30 '25 10:10

crispengari