473,788 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter pack difficulty

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(exp and=1, fill=X, side=TOP, anchor=N)

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

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

t.mainloop()

Sep 12 '07 #1
4 4176
On Wednesday 12 September 2007 13:22, Simon Forman wrote:
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
>
Thanks in advance,
~Simon

# Tkinter script
from Tkinter import *

t = Tk()

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

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

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

t.mainloop()
Sep 12 '07 #2
On Sep 12, 6:22 pm, Simon Forman <sajmik...@gmai l.comwrote:
Hi all,

SnipAny 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(fil l=X, side=TOP, anchor=N)

body = Frame(t, bg="grey")
body.pack(expan d=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

Sep 12 '07 #3
In article <11************ **********@g4g2 000hsf.googlegr oups.com>,
Simon Forman <sa*******@gmai l.comwrote:
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
Sep 12 '07 #4
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

Sep 17 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2103
by: John Fouhy | last post by:
Hi all, I have been attempting to change the title bar icon of my Tkinter applications in Windows XP. Chasing round in google, I discovered: - This is a common difficulty. - There aren't any good answers. Eventually, I stumbled across a link to this: http://www.tcl.tk/man/tcl8.4/TkCmd/wm.htm#M18
1
2977
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the scroll-bars work as advertised. That's great. However, if you click the Left Mouse button, it calls code which expands the width of the canvas by 100 pixels. The area being viewed expands correspondingly..... BUT I DON'T WANT IT TO!!
0
3587
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the window grows. It doesn't currently do this. although, if the window shrinks smaller than the...
1
3600
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings Panel. I just want it to bring up one. Is there an easy way to do that?
2
2332
by: jim-on-linux | last post by:
py help, The file below will run as a stand alone file. It works fine as it is. But, when I call it from another module it locks my computer, The off switch is the only salvation. This module when run as a stand alone, it will
2
4143
by: Kevin Walzer | last post by:
I'm trying to construct a simple Tkinter GUI and I'm having trouble with getting the value of an entry widget and passing it onto a callback function. But I'm not grokking variable scope correctly. I have two functions. Here are the relevant excerpts: def makeGUI(): ###lots of code to set up toplevel windows
1
1874
by: Kevin Walzer | last post by:
I'm trying to create a custom Tkinter widget class, and I'm having some difficulty getting it set up properly. The class is called MacToolbar, saved in its own module MacToolbar.py, and imported with this statement: import MacToolbar Here is the relevant portion of the class:
4
2173
by: MartinRinehart | last post by:
Everything I've read about Tkinter says you create your window and then call its mainloop() method. But that's not really true. This is enough to launch a default window from the console: Google's great, but it has no truth meter. Do I inherit from Frame? Or is that a big mistake. (Both positions repeated frequently.) Do I use Tk() or toplevel()? (Support for both and if a cogent explanation of the differences exists, I didn't find it.)
3
3918
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new window respectively having
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.