Tkinter: Why aren't my widgets expanding when I resize the window?

Rick Johnson rantingrickjohnson at gmail.com
Mon Mar 5 14:09:07 CET 2012


On Mar 5, 1:12 am, John Salerno <johnj... at gmail.com> wrote:
> I can't seem to wrap my head around all the necessary arguments
> for making a widget expand when a window is resized.

You will need to configure the root columns and rows also because the
configurations DO NOT propagate up the widget hierarchy! Actually, for
this example, I would recommend using the "pack" geometry manager on
the frame. Only use grid when you need to use grid. Never use any
functionality superfluously! Also, you should explicitly pack the
frame from OUTSIDE frame.__init__()!

## START CODE ##
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import BOTH, YES

class AppFrame(ttk.Frame):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)
        self._create_widgets()

    def _create_widgets(self):
        entry = ttk.Entry(self)
        entry.grid(row=0, column=1, sticky='nsew')
        label = ttk.Label(self, text='Name:')
        label.grid(row=0, column=0, sticky='nsew')

root = tk.Tk()
root.title('Test Application')
frame = AppFrame(root, borderwidth=15, relief='sunken')
frame.pack(fill=BOTH, expand=YES)
root.mainloop()
## END CODE ##

> Is there something wrong with the sticky argument, maybe? The
> tutorial I'm reading says they can be strings, but it also uses what
> appears to be a tuple of constants like this: sticky=(N, S, E, W) --
> but that didn't work either.

I always use either:

 sticky='nswe'
 sticky=N+S+W+E



More information about the Python-list mailing list