Making Gridded Widgets Expandable

Eric Brunel see.signature at no.spam
Mon Jul 30 10:46:35 EDT 2007


On Mon, 30 Jul 2007 15:59:21 +0200, Jim <jscrerar at compuserve.com> wrote:

> Hi,
> I'm looking at page 548 of Programming Python (3rd Edition) by Mark
> Lutz.
> The following GUI script works with no problem, i.e., the rows and
> columns expand:
> =================================================================
> # Gridded Widgets Expandable page 548
>
> from Tkinter import *
> colors = ["red", "white", "blue"]
>
> def gridbox(root):
>     Label(root, text = 'Grid').grid(columnspan = 2)
>     r = 1
>     for c in colors:
>         l = Label(root, text=c, relief=RIDGE, width=25)
>         e = Entry(root, bg=c,   relief=SUNKEN, width=50)
>         l.grid(row=r, column=0, sticky=NSEW)
>         e.grid(row=r, column=1, sticky=NSEW)
>         root.rowconfigure(r, weight=1)
>         r += 1
>     root.columnconfigure(0, weight=1)
>     root.columnconfigure(1, weight=1)
>
> root = Tk()
> gridbox(Toplevel(root))
> Button(root, text="Quit", command=root.quit).grid()
> mainloop()
> =================================================================
> However, the following GUI script using class does not expand rows and
> columns:
> =================================================================
> # Gridded Widgets Expandable 2
>
> from Tkinter import *
> colors = ["red", "white", "blue"]
>
> class GUI(Frame):
>     def __init__(self,master):
>         Frame.__init__(self,master)
>         self.grid()
>         self.gridbox()
>
>     def gridbox(self):
>         Label(self, text = 'Grid').grid(columnspan = 2)
>         r = 1
>         for c in colors:
>             l = Label(self, text=c, relief=RIDGE, width=25)
>             e = Entry(self, bg=c,   relief=SUNKEN, width=50)
>             l.grid(row=r, column=0, sticky=NSEW)
>             e.grid(row=r, column=1, sticky=NSEW)
>             self.rowconfigure(r, weight=1)
>             r += 1
>         self.columnconfigure(0, weight=1)
>         self.columnconfigure(1, weight=1)
>
> root = Tk()
> root.title("Gridded Widgets Expandable")
> app = GUI(root)
> Button(root, text="Quit", command=root.quit).grid()
> root.mainloop()
> =================================================================
> What am I missing?

Hint: when you get this kind of problem, the problem is usually that an  
intermedaite Frame does not expand as it does. Since you have only one  
Frame here (your GUI instance), you should already have the solution. If  
you don't, or in case you get this kind of problem later with a more  
complex layout, adding a visible border on all Frame's usually helps.  
Here, for example, you can't do:
Frame.__init__(self,master, border=2, bg='green')
in GUI.__init__, and this will definitely show that the Frame does not  
resize itself when you resize the window. So the solution is clear: you  
should add a sticky option to your self.grid in GUI.__init__ and the  
corresponding (row|column)_configure(..., weight=1) on the window.

BTW, why do you create a sub-class of Frame (yes: this is a trick  
question)?

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list