Mailing List Archive

newbie tkinter -- radio buttons
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.]

This works, but the problem is I can't set the first one to being
"selected", nor can I change the new color to be "selected"... it looks
really dorky to have no colors selected when you start and when you add
new colors. You have to *select* a color to start the radio buttons
working [after that, they're fine and switch properly among them.]

So I changed to this, so I could call the select() method:

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.

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?!?!]
Integrate add_command and .pack() so they don't stomp on eachother
Be able to temp = Separator(self.color_menu)

I'm sure each of these have very simple answers...

Thanx!

Kiki
newbie tkinter -- radio buttons [ In reply to ]
Hi, Cynthia--

Welcome to the great Tkinter adventure!

Cynthia Pettit <kiki@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@havenrock.com