Radio-button in Tkinter

Greg McFarlane gregm at iname.com
Thu Apr 27 08:25:13 EDT 2000


Shah,

This is a well-known feature of Tkinter.Variable - you must keep a
reference or the Tcl variable is destroyed.

In your case you will need to keep a reference to v1 and and v2.  If
the code snippet you supplied is from a method in a class, use self.v1
and self.v2 instead of just v1 and v2.  If the code is in a function,
you will have to make v1 and v2 globals.

Aside:  Why didn't you supply us with a snippet that we could run,
rather than just cut out a bit from your code?  There would be two
benefits to doing this:  First, myself and others would not have to
work out how to recreate the bits that you have left out (such as
frame3).  Secondly, if you had done what I did (and John Grayson, it
would appear) and run the code at the top level, rather than in a
function, you would have noticed that the problem went away - you
probably would have then realise what the cause was and would not have
had to post to the news group in the first place.

Moral - if you have code that does not behave as you expect, create
the absolute smallest runnable program that demonstrates the problem. 
If that does not lead you to the solution, post it.

Greg

On 22 Apr, ns56645 wrote:
> In the following code the radiobuttons that have been set are not seen
> as default selected in the output.
> For e.g         v1.set(3) # initialize should set the radiobutton with
> text 'BOTH' as default.
> 
> 
> What could be the wrong with the code?
> Please help?
> 
> Shah
> 
> 
> #######        Frame 3  Radio Buttons
> ##########################################################
> 
>         # Create the "Location" contents of the page.
>         group = Pmw.Group(frame3, tag_text = 'Location')
>         group.pack(fill = 'both', expand = 1, padx = 15, pady = 10)
> 
>         v1 = IntVar()
>         v1.set(3) # initialize
>         rb1 = Radiobutton(group.interior(), text = 'SWT', variable=v1,
> value=1)
>         rb1.grid(row = 0, column = 0)
>         rb2 = Radiobutton(group.interior(), text = 'WHS', variable=v1,
> value=2)
>         rb2.grid(row = 0, column = 1)
>         rb3 = Radiobutton(group.interior(), text = 'BOTH', variable=v1,
> value=3)
>         rb3.grid(row = 0, column = 2)
> 
> 
>         # Create the "Level" contents of the page.
>         group = Pmw.Group(frame3, tag_text = 'Level')
>         group.pack(fill = 'both', expand = 1, padx = 15, pady = 10)
> 
>         v2 = IntVar()
>         v2.set("13") # initialize
>         rb11 = Radiobutton(group.interior(), text = 'Grad', variable=v2,
> value=11)
>         rb11.grid(row = 0, column = 0)
>         rb12 = Radiobutton(group.interior(), text = 'UnderGrad',
> variable=v2, value=12)
>         rb12.grid(row = 0, column = 1)
>         rb13 = Radiobutton(group.interior(), text = 'BOTH', variable=v2,
> value=13)
>         rb13.grid(row = 0, column = 2)
> 
> 

-- 
Greg McFarlane     INMS Telstra Australia     gregm at iname.com




More information about the Python-list mailing list