Tkinter Grid manager question redux

Gabe Newcomb Gabe.Newcomb at noetix.com
Thu Jul 25 14:30:23 EDT 2002


(Re-posting, since posting as a RE: message didn't seem to help my cause
:))

This has gotten me the answers I need (thanks), but for one thing: I
can't seem to get sticky to position a widget within a cell. I'm
wondering what I could be doing wrong. Try this little example:

root = Tk()
f1 = Frame(root)
f2 = Frame(root)
f1.grid(row=0,column=0,sticky=N+E+W+S)
f1.grid(row=0,column=1,sticky=N+E+W+S)
l1 = Label(f1,text='L1 here',relief=SUNKEN)
l2 = Label(f2,text='L2 here',relief=SUNKEN)
l1.grid(sticky=W)
l2.grid(sticky=E)


I would expect it to create a root window with two frames next to each
other (it does that). In each frame, the label should appear, and the
one on the left should be all the way to the left of the frame, while
the one in the right frame should be all the way to the right of the
frame.

In practice, however, both labels end up on the opposite sides of the
frames (the left one is on the right side of the frame, and vice-versa.

Am I doing something wrong here, or do I need to approach this a
different way?

FYI:
If I try fixing the size of the frames and then using
frame.grid_propagate(0) for each frame, the labels end up in the center
of each frame.


Thanks,
Gabe


-----Original Message-----
From: jepler at unpythonic.net [mailto:jepler at unpythonic.net]
Sent: Tuesday, July 23, 2002 7:56 PM
To: Gabe Newcomb
Cc: python-list at python.org
Subject: Re: Tkinter Grid manager question


By default, a frame ignores the width= and height= it is given, and
instead
calculates the amount of space needed by the widgets it manages via grid
or
pack.

You can tell the grid manager that it should not behave in this way, and
should instead take the width= and height= you select for the frame and
parcel that space out to the managed widgets:
    f1.grid_propagate(0)
now, f1 will be 900x100

Similarly, if you "grid" things into f1_1 you need to run
    f1_1.grid_propagate(0)
if you "pack" things into f1_1, you need to run
    f1_1.pack_propagate(0)

If you want to make sure the rows are the same width, you need to set
the weight (again on the master):
    for row in range(4):
	f1.grid_rowconfigure(row, weight=1)
now, f1_1, f1_2, f1_3, and f1_4 will be configured to get equal shares
of
the height of f1.

If you still don't get a layout exactly 225 pixels wide, this is
probably
because of the borderwidth.  For instance, if you configure a frame
width a
thick border and then make another frame inside, the inner frame is
smaller (shifting to tcl since that's easier to do):
    %frame .f -bd 25 -relief raised -width 100 -height 100
    frame .f.f
    pack prop .f 0
    pack .f
    pack .f.f -expand 1 -fill both
    wm geo .
    winfo geo .f
    winfo geo .f.f
. and .f have geometries of 100x100, while .f.f is only 50x50, due to
the
border of .f.

Geometry management in Tk is a somewhat complex issue, and you have to
expect to play with the options before you get what you want.

Usually you will want to have as few intervening levels of frames as
possible.

Jeff

-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list