Next / Previous / Contents / Shipman's homepage

4.4. Making the root window resizeable

Do you want to let the user resize your entire application window, and distribute the extra space among its internal widgets? This requires some operations that are not obvious.

It's necessary to use the techniques for row and column size management, described in Section 4.3, “Configuring column and row sizes”, to make your Application widget's grid stretchable. However, that alone is not sufficient.

Consider the trivial application discussed in Section 2, “A minimal application”, which contains only a Quit button. If you run this application, and resize the window, the button stays the same size, centered within the window.

Here is a replacement version of the .__createWidgets() method in the minimal application. In this version, the Quit button always fills all the available space.

    def createWidgets(self):
        top=self.winfo_toplevel()                1
        top.rowconfigure(0, weight=1)            2
        top.columnconfigure(0, weight=1)         3
        self.rowconfigure(0, weight=1)           4
        self.columnconfigure(0, weight=1)        5
        self.quitButton = tk.Button(self, text='Quit', command=self.quit)
        self.quitButton.grid(row=0, column=0,    6
            sticky=tk.N+tk.S+tk.E+tk.W)
1

The “top level window” is the outermost window on the screen. However, this window is not your Application window—it is the parent of the Application instance. To get the top-level window, call the .winfo_toplevel() method on any widget in your application; see Section 26, “Universal widget methods”.

2

This line makes row 0 of the top level window's grid stretchable.

3

This line makes column 0 of the top level window's grid stretchable.

4

Makes row 0 of the Application widget's grid stretchable.

5

Makes column 0 of the Application widget's grid stretchable.

6

The argument sticky=tk.N+tk.S+tk.E+tk.W makes the button expand to fill its cell of the grid.

There is one more change that must be made. In the constructor, change the second line as shown:

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
        self.createWidgets()

The argument sticky=tk.N+tk.S+tk.E+tk.aW to self.grid() is necessary so that the Application widget will expand to fill its cell of the top-level window's grid.