Making Gridded Widgets Expandable

J

Jim

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?
Thanks,
Jim
 
E

Eric Brunel

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
 
J

Jim

From: Jim
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?

In the first, your gridbox has Toplevel(root) as its master, causing it
to be created in a new window. In the second, it has Frame(root) as its
master, which does not create a new window. Changing Frame to Toplevel
in the class statement and the call to __init__ causes them to act
identically.

Thank you Bill Hamilton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top