newbie tkinter -- radio buttons

Matt Gushee mgushee at havenrock.com
Sun Jun 20 12:48:11 EDT 1999


Hi, Cynthia--

Welcome to the great Tkinter adventure!

Cynthia Pettit <kiki at pixar.com> writes:

> Okay, you get your choice of answering any single one of these questions
> to solve my woes! :)
> 
> I'm making a color menu in a drawing program.  It allows you to add
> named colors as you go.
> 
> At first, I was adding colors this way:
> 
>   self.color_menu.add_radiobutton(label=color_name,
>    value = color_name,
>    variable = self.current_color)
> 
> [Note: a couple other issues: value and variable don't seem to work.
> When I choose the button, it does not change the value of
> self.current_color.]

I bet you're trying to do this:

	new_color = self.current_color

I wish the documentation were clearer on this inherently confusing
point. The 'variable' attribute of a Tkinter widget does *not* refer
to a Python variable. It refers to a Python *object* which acts as the 
interface to a Tk variable. So you need to

1) Create an instance of IntVar, StringVar, DoubleVar, or BooleanVar,
e.g.:

	self.current_color = StringVar()

and

2) Use the get() and set() methods to access the value:

	new_color = self.current_color.get()

the menu constructor command is fine as you wrote it.

>   temp = Radiobutton(self.color_menu, text=color_name,
>    value = color_name,
>    variable = self.current_color)
>   temp.pack(anchor=W)
>   temp.select()

> ...

> But now, of course, this conflicts with other calls, like add_command
> and add_separator.  [eg "Add Color"]  The buttons look to be on top of
> eachother.  I think this is due to the .pack() calls not working with
> the add_radiobutton calls.

Well, they're not supposed to. The Menu class isn't designed to
contain other widgets; the radiobuttons, separators, etc. within menus
that are created with add_radiobutton are not class instances, they're
just components of the Menu instance. Hence they don't have their own
methods, and must be manipulated through the methods of the Menu
class.

Unless you want to go to the trouble of manually creating a menu from 
a Frame() class instance -- but unless you need to do something
extremely sophisticated and non-standard, I don't see why you would
need to do that.

> Here's a choice of things that if solved would save me [in order of
> preference]:
> 
> Call .select() on an add_radiobutton [and just how *does* one ever
> access these buttons ever again?!?!]

Haven't worked with Tkinter in a while, but IIRC, calling the set()
method of your radiobutton variable should do what you want.

Have you read the Tk manual page for the Menu class? If not, I'd highly
recommend doing so. It's a shame to have to learn two languages in
order to use one, but the Tkinter documentation has a ways to go
yet. Personally I find that understanding Tk widgets from a Tcl
perspective has saved me a lot of unproductive flailing around w/
Tkinter.

Hope this helps a bit.

Matt Gushee
Portland, Maine, USA
mgushee at havenrock.com





More information about the Python-list mailing list