Tkinter pack difficulty

S

Simon Forman

Hi all,

I realize this is more of a Tk question than a python one, but since
I'm using python and don't know Tcl/Tk I figured I'd ask here first
before bugging the Tcl folks.

I am having a terrible time trying to get a pack() layout working.

I have three frames stacked top to bottom and stretching across the
master window from edge to edge.

Crude ASCII Art rendition of the frames:

============
| header |
------------
| body |
------------
| log |
============


I want the header and log frames to have a fixed height (and stick to
the top and bottom, respectively, of the master frame) and the body
frame to expand to fill the rest of the space, for instance if the
window is maximized.

Here is a simple script that /almost/ does what I want. I've been
tweaking the pack() options for three hours and I just can't seem to
get the effect I want. This /can't/ really be this hard can it?

If you run the script, be aware that since there are only frame
widgets the window will initially be very very tiny. If you expand or
maximize the window you'll see a thin black frame at the top, yay, a
thin white frame at the bottom, yay, but the middle grey "body" frame
will NOT span the Y axis, boo.

It's there, and stretches from side to side, but it refuses to stretch
top to bottom. Adding a widget (say, a Text) doesn't help, the light
grey non-frame rectangles remain.

My investigations seem to indicate that the light grey bars are part
of something the Tk docs call a "parcel" that each slave widget gets
packed into. Apparently the "header" and "log" frames don't use their
entire parcels, but I don't know how to get the parcels themselves to
"shrinkwrap" to the size of the actual Frame widgets.

In any event, my head's sore and I'm just about ready to take out
some graph paper and use the grid() layout manager instead. But I
really want the automatic resizing that the pack() manager will do,
rather than the static layout grid() will give me.

Any thoughts or advice?

Thanks in advance,
~Simon

# Tkinter script
from Tkinter import *

t = Tk()

header = Frame(t, bg="black", height=10)
header.pack(expand=1, fill=X, side=TOP, anchor=N)

body = Frame(t, bg="grey")
body.pack(expand=1, fill=BOTH, anchor=CENTER)

log = Frame(t, bg="white", height=10)
log.pack(expand=1, fill=X, side=BOTTOM, anchor=S)

t.mainloop()
 
J

jim-on-linux

Hi all,

I realize this is more of a Tk question than a python one, but
since I'm using python and don't know Tcl/Tk I figured I'd ask here
first before bugging the Tcl folks.

I am having a terrible time trying to get a pack() layout working.

I have three frames stacked top to bottom and stretching across the
master window from edge to edge.

Crude ASCII Art rendition of the frames:

============

| header |

------------

| body |

------------

| log |

============


I want the header and log frames to have a fixed height (and stick
to the top and bottom, respectively, of the master frame) and the
body frame to expand to fill the rest of the space, for instance if
the window is maximized.

Here is a simple script that /almost/ does what I want. I've been
tweaking the pack() options for three hours and I just can't seem
to get the effect I want. This /can't/ really be this hard can it?

If you run the script, be aware that since there are only frame
widgets the window will initially be very very tiny. If you expand
or maximize the window you'll see a thin black frame at the top,
yay, a thin white frame at the bottom, yay, but the middle grey
"body" frame will NOT span the Y axis, boo.

It's there, and stretches from side to side, but it refuses to
stretch top to bottom. Adding a widget (say, a Text) doesn't help,
the light grey non-frame rectangles remain.

My investigations seem to indicate that the light grey bars are
part of something the Tk docs call a "parcel" that each slave
widget gets packed into. Apparently the "header" and "log" frames
don't use their entire parcels, but I don't know how to get the
parcels themselves to "shrinkwrap" to the size of the actual Frame
widgets.

In any event, my head's sore and I'm just about ready to take out
some graph paper and use the grid() layout manager instead. But I
really want the automatic resizing that the pack() manager will do,
rather than the static layout grid() will give me.

Any thoughts or advice?

Sorry I can't help you with pack, I don't use it anymore.

I am able to do everything with grid that I can do with pack.
Once I learned to use grid I liked it better than pack.
Spend some time to learn grid you may like it.
I'm sure others will disagree, but for me it works for everything I
need.

jim-on-linux
http://inqvista.com
 
T

Tony

Hi all,

Snip> Any thoughts or advice?

Thanks in advance,
~Simon

This seems to do what you want, the difference is that the expand
option is left out top and bottom, also I increased height and put in
a width value as well:
from Tkinter import *

t = Tk()

header = Frame(t, bg="black", height=30, width = 10)
header.pack(fill=X, side=TOP, anchor=N)

body = Frame(t, bg="grey")
body.pack(expand=1, fill=BOTH, anchor=CENTER)

log = Frame(t, bg="white", height=30, width = 10)
log.pack( fill=X, side=BOTTOM, anchor=S)

t.mainloop()

Ciao
Tony
 
R

Russell E. Owen

Simon Forman said:
Hi all,

I realize this is more of a Tk question than a python one, but since
I'm using python and don't know Tcl/Tk I figured I'd ask here first
before bugging the Tcl folks.

I am having a terrible time trying to get a pack() layout working.

I have three frames stacked top to bottom and stretching across the
master window from edge to edge.

Crude ASCII Art rendition of the frames:

============
| header |
------------
| body |
------------
| log |
============


I want the header and log frames to have a fixed height (and stick to
the top and bottom, respectively, of the master frame) and the body
frame to expand to fill the rest of the space, for instance if the
window is maximized.

Here is a simple script that /almost/ does what I want. I've been
tweaking the pack() options for three hours and I just can't seem to
get the effect I want. This /can't/ really be this hard can it?

If you run the script, be aware that since there are only frame
widgets the window will initially be very very tiny. If you expand or
maximize the window you'll see a thin black frame at the top, yay, a
thin white frame at the bottom, yay, but the middle grey "body" frame
will NOT span the Y axis, boo.

It's there, and stretches from side to side, but it refuses to stretch
top to bottom. Adding a widget (say, a Text) doesn't help, the light
grey non-frame rectangles remain.

My investigations seem to indicate that the light grey bars are part
of something the Tk docs call a "parcel" that each slave widget gets
packed into. Apparently the "header" and "log" frames don't use their
entire parcels, but I don't know how to get the parcels themselves to
"shrinkwrap" to the size of the actual Frame widgets.

In any event, my head's sore and I'm just about ready to take out
some graph paper and use the grid() layout manager instead. But I
really want the automatic resizing that the pack() manager will do,
rather than the static layout grid() will give me.

The grid layout manager is the obvious choice for this and it is
dynamic. (There is a place geometry manager that works with coordinates,
but I don't recommend that for this case).

Grid the top/middle/bottom frame in row = 0/1/2, column = 0
then use row_configure to set the weight of the middle frame to 1.
You may also have to set sticky to "news" when you grid all the frames.

In general I suggest you use grid unless it's something simple like a
row of buttons.

But never try to both grid and pack anything into the in the same parent
widget. You'll get a crash or an infinite loop. (You can grid frames
that have widgets packed inside them, but you can't grid grid some items
into a frame and then pack some more into the same frame)

-- Russell
 
S

Simon Forman

Thanks everyone for the incredibly helpful replies! I got the effect
I wanted, no problem. I don't know why I didn't think to remove the
expand option. I thought the sticky option would constrain the
expansion.

Thanks again,
~Simon
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top