tkinter puzzler

Peter Otten __peter__ at web.de
Thu May 12 04:44:12 EDT 2005


Paul Rubin wrote:

I think you are missing the columnconfigure()/rowconfigure() methods as
Martin Franklin pointed out. Anyway, here is some code to illustrate the
matter. I've found it helpful to use "false" colors to see what's going on.
the yellow 'main' frame contains the red 'north' and the blue 'south'
frame.

import Tkinter as tk

root = tk.Tk()
main = tk.Frame(root, bg="yellow")
main.pack(expand=True, fill="both")
main.columnconfigure(0, weight=1)
main.rowconfigure(0, weight=1)

north = tk.Frame(main, bg="red")
north.columnconfigure(1, weight=1)
for row, text in enumerate("alpha beta gamma delta".split()):
    label = tk.Label(north, text=text)
    label.grid(row=row, column=0, sticky="NW")
    entry = tk.Entry(north)
    entry.grid(row=row, column=1, sticky="NEW")
    button = tk.Button(north, text=text.upper())
    button.grid(row=row, column=2, sticky="NW")
    north.rowconfigure(row, weight=1)
north.grid(column=0, row=0, sticky="NEW")

south = tk.Frame(main, bg="blue")
stop = tk.Button(south, text="Stop")
go = tk.Button(south, text="Go")
stop.grid(column=0, row=0)
go.grid(column=2, row=0)
# the empty column takes all the extra space
south.columnconfigure(1, weight=1) 
south.grid(column=0, row=1, sticky="NSEW")

root.mainloop()

Peter




More information about the Python-list mailing list