[Tutor] Tkinter Geometry Management and Other Basics

Alan Gauld alan.gauld at btinternet.com
Fri Mar 20 00:57:04 CET 2009


"Wayne Watson" <sierra_mtnview at sbcglobal.net> wrote

> As I understand it, there are three geometry managers:
> Grids, Pack and Place. Only the first two are of interest.

Yes and a new Form one coming in the next Tk release...

> Is it possible to mix them? I don't think so,

Yes but not in a single Frame.

But my normal approach is to defie my top window
using multiple frames. Each frame is packed into the
top window. But within each subframe I may use either
the packer or the grid depemnding on the widgets inside.
So long as yyou stick to a single style inside a Frame
you can mix n match to suit the layout.

> However, if one is used for frameA and another for frameB,
> can they both be used with in a parent frame, which has its
> own geometry (grid or pack)?

Absolutely.

> Suppose I want a parent frame to have a title and below it
> a 2 column grid with say a Label and Entry. I put the latter
> in a frame combo in a frame, and attach it to the parent.

I'd have two frames packed into the window (packer
stacks vertically by default). In the top Frame I'd pack the
title - centred is default with pack. In the second frame I'd
use grid with a single row, one cell with label the other with
the entry.

> First, the resulting dialog looks like this:
>
> -------------------------------------------------------
>        Geographic Location
>    Latitude:BOXLongitude:BOX
> Zenith x:Zenithy:BOXZenith Pixel Position
>
>           OK   Cancel   <- Std buttons
> ---------------------------------------------------------
> BOX = a rectangle for data entry. There's very little separation 
> between labels and entries. Title labels are in bold.
>
> Stuff is kind of jammed together,

padx and pady should fix that. Take a look at the Exploring Layout
section in my GUI programming topic for more ideas on that. It uses
the packer exclusively but should give you some clues.

> My guess is that I need to use the weight concept available
> with Grid, columnconfigure and rowconfigure to justify and
> open up the data entry rows.

You are best just experimenting. The way I do that is to write the
simplest possible GUI with just a single frame and a button or
label in it.

Something like:

##############
# testTk.py
from Tkinter import *

top = Tk()
w = Button(top, text="Button")
w.pack()
top.mainloop()
##############

Just play with the pack or grid options and observe the effect
If you have the file open in an editor and name the file as .pyw
you can then easily run the file from explorer and have multiple
versions displayed at the same time to compare results.


> Perhaps there's a geometry tutor somewhere on the web.

Try the Tcl/Tk web sites they tend to be good for that kind of stuff.
Its easy to translate the Tk command to Tkinter.

HTH,


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list