Mailing List Archive

Tkinter: radiobutton
Newbie speaks: I'm sure there's an elegant solution to this, but I just
can't find it, and my head hurts from reading man pages...

I have a color menu that has "Add Color" and "Change Color", and an
appended list of user-created colors as radiobuttons.

What I would like to do is when the user chooses "Change Color", the new
color overwrites the current color selected in the radiobutton list.

The problem is I don't know how to find out which button is selected.
self.color_menu.entryconfig("active"...) configures the "Change Color"
button!! Arg! The index is otherwise implicit...

A group of radiobuttons are associated by the fact that they share a
variable. When that variable changes, the states of the radio buttons
change. Certainly there must be a way to find out the index of the
selected radiobutton in a group of radiobuttons and modify the contents
[eg "value", "activebackground", etc.] of it...

Thanx, and sorry if this was an obvious question.

Kiki
Tkinter: radiobutton [ In reply to ]
kiki <kiki@pixar.com> wrote:
: Newbie speaks: I'm sure there's an elegant solution to this, but I just
: can't find it, and my head hurts from reading man pages...

: I have a color menu that has "Add Color" and "Change Color", and an
: appended list of user-created colors as radiobuttons.

: What I would like to do is when the user chooses "Change Color", the new
: color overwrites the current color selected in the radiobutton list.

: The problem is I don't know how to find out which button is selected.
: self.color_menu.entryconfig("active"...) configures the "Change Color"
: button!! Arg! The index is otherwise implicit...

: A group of radiobuttons are associated by the fact that they share a
: variable. When that variable changes, the states of the radio buttons
: change. Certainly there must be a way to find out the index of the
: selected radiobutton in a group of radiobuttons and modify the contents
: [eg "value", "activebackground", etc.] of it...

: Thanx, and sorry if this was an obvious question.

: Kiki

No, it's not an obvious question at all. Unfortunately, you need to
track the index when you create the menu. By default, the first call
starts at 1, because the "tear-off" is 0. If the menu is created with
the -tearoff=0 option, then the index starts at 0

This example creates a menu where the first item is a button (which
when pressed doesn't do anything) used as a Label to hold the current
color, the other items (radiobuttons) after it change the label.

menu = Menu(menubutton)
def change_red(m=menu):
m.entryconfig(1, label='Color: red')
menubutton['menu'] = menu
# the first is effectively a "Label"
menu.add_command(label='Color: ') # index 1
menu.add_radiobutton(label='Change red', command=change_red) # index 2

Maybe it's time I give those menu entry classes to Guido (that does
this for you as class instances).

-Arcege
Tkinter: radiobutton [ In reply to ]
"Michael P. Reilly" wrote:

>
> No, it's not an obvious question at all. Unfortunately, you need to
> track the index when you create the menu. By default, the first call
> starts at 1, because the "tear-off" is 0. If the menu is created with
> the -tearoff=0 option, then the index starts at 0
>

Bleh. Oh well. Kind of a shame, since I'd rather just have the color value
put into the color variable, not an index with my own list I have to maintain
myself.

Well, cool, at least I have a solution.

Thanx so much for your help!

Kiki
Tkinter: radiobutton [ In reply to ]
kiki <kiki@pixar.com> wrote:
: "Michael P. Reilly" wrote:

:>
:> No, it's not an obvious question at all. Unfortunately, you need to
:> track the index when you create the menu. By default, the first call
:> starts at 1, because the "tear-off" is 0. If the menu is created with
:> the -tearoff=0 option, then the index starts at 0
:>

: Bleh. Oh well. Kind of a shame, since I'd rather just have the color value
: put into the color variable, not an index with my own list I have to maintain
: myself.

: Well, cool, at least I have a solution.

: Thanx so much for your help!

Kiki,

I just uploaded a module I made for another project (and never remembered
to give to Guido).

The URL is ftp://starship.python.net/pub/crew/arcege/modules/MenuEntry.py.

In that module, you get five usable classes (and one super class):
MenuCascade, MenuCommand, MenuCheckbutton, MenuRadiobutton, MenuSeparator.

You get the following methods:
item.activate()
item.cget(option)
item.configure([cnf={...},] [key=value,]...)
item.config - same as configure
item.invoke()
item.yposition()

These map to methods in the Menu class, but the MenuEntry object knows
which index it is.

-Arcege