0

In my small PyGObject/GTK3 application I try to create a (sub-)menu that consists of RadioMenuItems.

I create the menu as follows in the main window's __init__:

menu_item = None
self.menu = Gtk.Menu()
self.menu_items = {}
for i in range(1,5):
   menu_item = Gtk.RadioMenuItem(menu_item)
   menu_item.set_label(str(i))
   self.menu_items[i] = menu_item
   self.menu.append(self.menu_items[i])
   self.menu_items[i].connect("activate",
                        self.on_menu_select,
                        i)
   self.menu_items[i].show()

and then show it in the button_press_event handler using

def button_press(self, widget, event, data=None):
    for i in range(1,5):
        self.menu_items[i].set_active(False)
    self.menu_items[2].set_active(True)
    self.menu.popup(None, None, None, None,
                            event.button, event.time)

When I open the menu for the first time, menu item "2" is correctly the only one selected. When I then - for example - choose menu item "3" and open the menu again, both menu items 2 and 3 are selected. What am I doing wrong?

gnirx
  • 197
  • 1
  • 1
  • 8

1 Answers1

1

You have to put the radio menu items in a group with each other, or else they won't know to deselect themselves when another one is selected:

self.menu = Gtk.Menu()
self.menu_items = list()
group = []
for i in range(1,5):
    menu_item = Gtk.RadioMenuItem.new_with_label(group, str(i))
    group = menu_item.get_group()
    self.menu_items[i] = menu_item
    self.menu.append(menu_item)
    menu_item.connect("activate", self.on_menu_select, i)
    menu_item.show()

self.menu_items[2].set_active(True)

Note that this line selects item number 2, you don't have to do it yourself, so your button press handler can simply be:

def button_press(self, widget, event, data=None):
    self.menu.popup(None, None, None, None,
                    event.button, event.time)
Sahil
  • 315
  • 5
  • 21
ptomato
  • 56,175
  • 13
  • 112
  • 165
  • Thanks, that solves my problem. I thought I was putting them in the same group by passing the old menu_item into the constructor of the new one using menu_item = Gtk.RadioMenuItem(menu_item), but apparently I read the documentation wrong. Thank you! – gnirx Apr 29 '12 at 12:36