Newbie Tkinter questions

Peter Abel p-abel at t-online.de
Fri Jun 27 10:02:25 EDT 2003


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




More information about the Python-list mailing list