GTK+: How do I process RadioMenuItem choice without marking it chosen? And vise versa

Posted by eugene.shatsky on Stack Overflow See other posts from Stack Overflow or by eugene.shatsky
Published on 2014-08-09T11:59:57Z Indexed on 2014/08/20 10:20 UTC
Read the original article Hit count: 166

Filed under:
|
|
|

In my program, I've got a menu with a group of RadioMenuItem entries. Choosing one of them should trigger a function which can either succeed or fail. If it fails, this RadioMenuItem shouldn't be marked chosen (the previous one should persist). Besides, sometimes I want to set marked item without running the choice processing function. Here is my current code:

# Update seat menu list
def update_seat_menu(self, seats, selected_seat=None):
    seat_menu = self.builder.get_object('seat_menu')
    # Delete seat menu items
    for menu_item in seat_menu:
        # TODO: is it a good way? does remove() delete obsolete menu_item from memory?
        if menu_item.__class__.__name__ == 'RadioMenuItem': seat_menu.remove(menu_item)
    # Fill menu with new items
    group = []
    for seat in seats:
        menu_item = Gtk.RadioMenuItem.new_with_label(group, str(seat[0]))
        group = menu_item.get_group()
        seat_menu.append(menu_item)
        if str(seat[0]) == selected_seat: menu_item.activate()
        menu_item.connect("activate", self.choose_seat, str(seat[0]))
        menu_item.show()

# Process item choice
def choose_seat(self, entry, seat_name):
    # Looks like this is called when item is deselected, too; must check if active
    if entry.get_active():
        # This can either succeed or fail
        self.logind.AttachDevice(seat_name, '/sys'+self.device_syspath, True)

Chosen RadioMenuItem gets marked irrespective of the choose_seat() execution result; and the only way to set marked item without triggering choose_seat() is to re-run update_seat_menu() with selected_seat argument, which is an overkill.

I tried to connect choose_seat() with 'button-release-event' instead of 'activate' and call entry.activate() in choose_seat() if AttachDevice() succeeds, but this resulted in whole X desktop lockup until AttachDevice() timed out, and chosen item still got marked.

© Stack Overflow or respective owner

Related posts about python

Related posts about gtk