A note on window responsiveness

We used .grid_propagate(False) in this program to ensure that our frames did not shrink to fit their contents, but rather stayed at a fixed height and width that we had specified while making the frames.

This served us well for this example, but this made our window and its content fixed in size. This is what you would typically call a non-responsive window.

Let us take a look at the program nonresponsive.py as an example of a non-responsive window. This program simply draws 10 buttons in a row:

from tkinter import Tk, Button
root = Tk()

for x in range(10):
btn = Button(root, text=x )
btn.grid(column=x, row=1, sticky='nsew')

root.mainloop()

Run this program and resize the window. These buttons are drawn on the root window and are not responsive. The buttons remain fixed in size. They do not adapt in size to change in the window size.  If the window size is made smaller, some of the buttons even disappear from view.

In contrast, let us take a look at the program responsive.py:

from tkinter import Tk, Button, Grid

root = Tk()

for x in range(10):
Grid.rowconfigure(root, x, weight=1)
Grid.columnconfigure(root, x, weight=1)
btn = Button(root, text=x )
btn.grid(column=x, row=1, sticky='nsew')

root.mainloop()

If you run this program and resize the window, you will see that the buttons respond by resizing themselves to fit the container root window. So what's the difference between the two previous pieces of code? We simply added these two lines to the second program:

Grid.rowconfigure(root, x, weight=1)
Grid.columnconfigure(root, x, weight=1)

These two lines add non-zero weights (weight=1) to the xth button widget in the container (root in the preceding example).

The key here is to understand the importance of weights. If we have two widgets, widget1 and widget2, and we assign them weights of 3 and 1, respectively. Now when you resize its parent, widget1 will take up 3/4th of the space, while widget2 will take up 1/4th of the space.

Here's the documentation of rowconfigure and columnconfigure:
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-config.html.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.172.130