Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Newbie Tkinter questions (http://www.velocityreviews.com/forums/t318908-newbie-tkinter-questions.html)

Doru-Catalin Togea 06-27-2003 10:02 AM

Newbie Tkinter questions
 
Hei!

I am trying to get up and working with Tkinter fast. I have some
documentation like Fredrik Lundh's An Introduction to Tkinter.

Can you please tell what I am missing in the following programme:

from Tkinter import *

################################################## ######################
# GLOBAL VARIABLES
################################################## ######################

root = Tk()
schools = [("HIO", "1"), ("NITH", "2"),]
rbSchools = None # initialize later on to a Radiobutton
# object
lArchiveNo = None # Label
eArchiveNo = None # Entry
v = StringVar()
v.set("0") # For the Radiobutton: no choice

################################################## ######################
# FUNCTIONS
################################################## ######################

def chooseSchool(value):
print "you have chosen Radiobutton no.", value

################################################## ######################
# MAIN PROGRAMME
################################################## ######################

lArchiveNo = Label(root, text = "Sak nr.: ")
lArchiveNo.grid(row = 0, column = 0)
lArchiveNo.pack(side=LEFT)

eArchiveNo = Entry(root, width = 20)
eArchiveNo.grid(row = 0, column = 1)
eArchiveNo.pack(side=LEFT)

# make the radio button for schools:
rbSchools = Radiobutton(root, name = "rbSchools")
#rbSchools.bind("<Button-1>", chooseSchool)
#print str(rbSchools)
for text, value in schools:
rbSchools = Radiobutton(root, text=text, variable=v, \
value=value, command=chooseSchool(value))
rbSchools.grid(row = 1, column = 0)
rbSchools.pack(anchor=W)

root.mainloop()

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).

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???)

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

Catalin

--

<<<< ================================== >>>>
<< We are what we repeatedly do. >>
<< Excellence, therefore, is not an act >>
<< but a habit. >>
<<<< ================================== >>>>


Paul Simmonds 06-27-2003 02:00 PM

Re: Newbie Tkinter questions
 
Doru-Catalin Togea <doru-cat@ifi.uio.no> wrote in message news:<mailman.1056708259.21345.python-list@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


Peter Abel 06-27-2003 02:02 PM

Re: Newbie Tkinter questions
 
Doru-Catalin Togea <doru-cat@ifi.uio.no> wrote in message news:<mailman.1056708259.21345.python-list@python.org>...
> Hei!
>
> I am trying to get up and working with Tkinter fast. I have some
> documentation like Fredrik Lundh's An Introduction to Tkinter.
>
> Can you please tell what I am missing in the following programme:
>
> from Tkinter import *
>
> ################################################## ######################
> # GLOBAL VARIABLES
> ################################################## ######################
>
> root = Tk()
> schools = [("HIO", "1"), ("NITH", "2"),]
> rbSchools = None # initialize later on to a Radiobutton
> # object
> lArchiveNo = None # Label
> eArchiveNo = None # Entry
> v = StringVar()
> v.set("0") # For the Radiobutton: no choice
>
> ################################################## ######################
> # FUNCTIONS
> ################################################## ######################
>
> def chooseSchool(value):
> print "you have chosen Radiobutton no.", value
>
> ################################################## ######################
> # MAIN PROGRAMME
> ################################################## ######################
>
> lArchiveNo = Label(root, text = "Sak nr.: ")
> lArchiveNo.grid(row = 0, column = 0)
> lArchiveNo.pack(side=LEFT)
>


grid and pack doen't work togather. Use either grid or pack.

> eArchiveNo = Entry(root, width = 20)
> eArchiveNo.grid(row = 0, column = 1)
> eArchiveNo.pack(side=LEFT)
>


grid and pack doe ....

> # make the radio button for schools:
> rbSchools = Radiobutton(root, name = "rbSchools")
> #rbSchools.bind("<Button-1>", chooseSchool)
> #print str(rbSchools)
> for text, value in schools:
> rbSchools = Radiobutton(root, text=text, variable=v, \
> value=value, command=chooseSchool(value))
> rbSchools.grid(row = 1, column = 0)
> rbSchools.pack(anchor=W)


grid and pack doe ....

>
> root.mainloop()
>
> 1) Is the grid used with the Label and the Entry the same as the grid in
> the for-loop?


Yes it is.

> (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).
>


But you have to use a variable for row or column and no constants.
Otherwise several widgets in each loopcount are in the same gridcell.

> 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???)
>


You have to declare a Tkintervariable and assign its name to the option
**variable**.
A group of Radiobuttons must have the same Tkintervariable. Furthermore you
have assign a value to the option **value*, where the variable's value will
be set to if the equivalent Radiobutton has been pressed by the user.

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


I think Lundh's introduction is very helpfull and detailed though it might
be not quite complete. He seems to me as one of the **Tkinter-Popes**.
He has some more information around Python, Tkinter etc. at
http://effbot.org/zone/.
In addition have a look on the doku at
http://www.nmt.edu/tcc/help/pubs/lang.html.

> Catalin


Regards
Peter


All times are GMT. The time now is 12:18 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.