Newbie Tkinter questions

Paul Simmonds psimmo60 at hotmail.com
Fri Jun 27 10:00:10 EDT 2003


Doru-Catalin Togea <doru-cat at ifi.uio.no> wrote in message news:<mailman.1056708259.21345.python-list at python.org>...
<code snipped>
Hello,

> 1) Is the grid used with the Label and the Entry the same as the grid in
> the for-loop? (it doesn't seem so, I want the Entry after the Label in the
> 1st row and the Radiobutton in the 2nd row, first cell or columnspan = 2).
Yes. Within the same window/frame, the same grid is used. See below,
and the appropriate section in the Introduction to Tkinter by Frederik
Lundh.

> 2) How do I capture the change on the Radiobutton? As it is now, the
> programme calls chooseSchool with the right value at creation time, not
> when I click the Radiobutton. (Or maybe it is called when I click the
> Radiobutton as well, but the event is captured and ignored before it
> reaches the Radiobutton???)
Linking a Radiobutton(or Checkbox, for that matter) to a String/IntVar
is generally to best way. Changing the Var automatically updates the
state of the Radio/Check.

>From the code, there are a few comments I'd make:

-The grid(, place) and pack geometry managers should not be used
within the same container widget (meaning Frame,Toplevel,etc). Tk will
happily spend the rest of your lifetime trying to find a placement
that all are happy with. I think what you want to use is the grid
manager with the 'sticky' option. See grid manager doc in the back of
the I2Tkinter

-In Python, names are bound to objects the first time they are used.
There is no need to bind them to objects you aren't going to use. It
looks like you're trying to declare names, which you don't need to do.

-This is a very minor point. However, when using Tkinter, you only
need to bind names to object you want to keep a track of. Labels, for
example, unless you want to change the contents at some point, can be
created without a name. Radiobuttons, too, if you've linked the button
state to a variable, don't need names. Tk will keep a track of them.
i.e.:
# Static label
Label(root, text="My label").grid(row=0,column=0,sticky=W)
# Radiobutton linked to IntVar
curstate=IntVar() # This is what matters
curstate.set(0)
Radiobutton(root,text="On/Off",value=1,variable=curstate).grid(column=2,row=0)
#But!
myentry=Entry(root) # Entrybox you'll want to access
myentry.grid(column=1,row=0)

-Names can only apply to one object at a time. In your code, you
rebind the rbSchools name 4 times. If you want to dynamically create
instances, append them to a list. It's more flexible. However, it
looks like what you really what is to give all your Radiobuttons the
same IntVar as a variable option. Then you dpn't need to give them
names at all. See previous point.

-If you want real control over your gridding of widgets, don't put
them in the same grid square. What will happen is that the first
widget will get put in the right place, and the second will be put
anywhere the manager considers the next best (usually the line below,
which is why it works right in your code).

> Thanks if you can help. Is there any documentation which is more detailed
> than Lundh's introduction?

IMO, between the Python Library Reference
(http://www.python.org/doc/lib/) and the Introduction you have, you
should have enough to start. Section II really is indispensible when
you're trying something new. Make sure to read the option list and
description for each new widget and experiment!

Alternatively read the Tk documentation, GIYF. However, for a newbie
it can be a little daunting. Get to know how the system works before
trying to translate Tk->Tkinter.

HTH,
Paul

> 
> Catalin




More information about the Python-list mailing list