Tkinter: radiobutton

Michael P. Reilly arcege at shore.net
Thu Jun 24 11:40:06 EDT 1999


kiki <kiki at 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





More information about the Python-list mailing list