def add(self, label, function, menu=None): item = RadioMenuItem(label=label) item.set_name(label) self.append(item) if menu: item.connect('activate', function, label, menu) else: item.connect('activate', function, label) item.show() self._menu_dict[label] = item
def __init__(self, commands, function, menu=None): SimpleMenu.__init__(self) self.main_item = RadioMenuItem(label=commands[0]) self._menu_dict[commands[0]] = self.main_item for command in commands[1:]: item = RadioMenuItem(self.main_item, label=command) item.set_name(command) self.append(item) if menu: item.connect('activate', function, command, menu) else: item.connect('activate', function, command) item.show() self._menu_dict[command] = item
def build_menu_radio_list( value_list, callback, pref_value=None, suffix=None, show_notset=False, notset_label='∞', notset_lessthan=0, show_other=False, ): """Build a menu with radio menu items from a list and connect them to the callback. Params: value_list [list]: List of values to build into a menu. callback (function): The function to call when menu item is clicked. pref_value (int): A preferred value to insert into value_list suffix (str): Append a suffix the the menu items in value_list. show_notset (bool): Show the unlimited menu item. notset_label (str): The text for the unlimited menu item. notset_lessthan (int): Activates the unlimited menu item if pref_value is less than this. show_other (bool): Show the `Other` menu item. The pref_value is what you would like to test for the default active radio item. Returns: gtk.Menu: The menu radio """ menu = Menu() group = None if pref_value > -1 and pref_value not in value_list: value_list.pop() value_list.append(pref_value) for value in sorted(value_list): item_text = str(value) if suffix: item_text += ' ' + suffix menuitem = RadioMenuItem(group=group, label=item_text) group = menuitem if pref_value and value == pref_value: menuitem.set_active(True) if callback: menuitem.connect('toggled', callback) menu.append(menuitem) if show_notset: menuitem = RadioMenuItem(group=group, label=notset_label) menuitem.set_name('unlimited') if pref_value and pref_value < notset_lessthan: menuitem.set_active(True) menuitem.connect('toggled', callback) menu.append(menuitem) if show_other: menuitem = SeparatorMenuItem() menu.append(menuitem) menuitem = MenuItem(_('Other...')) menuitem.set_name('other') menuitem.connect('activate', callback) menu.append(menuitem) return menu
def build_menu_radio_list(value_list, callback, pref_value=None, suffix=None, show_notset=False, notset_label='∞', notset_lessthan=0, show_other=False): """Build a menu with radio menu items from a list and connect them to the callback. Params: value_list [list]: List of values to build into a menu. callback (function): The function to call when menu item is clicked. pref_value (int): A preferred value to insert into value_list suffix (str): Append a suffix the the menu items in value_list. show_notset (bool): Show the unlimited menu item. notset_label (str): The text for the unlimited menu item. notset_lessthan (int): Activates the unlimited menu item if pref_value is less than this. show_other (bool): Show the `Other` menu item. The pref_value is what you would like to test for the default active radio item. Returns: gtk.Menu: The menu radio """ menu = Menu() group = None if pref_value > -1 and pref_value not in value_list: value_list.pop() value_list.append(pref_value) for value in sorted(value_list): item_text = str(value) if suffix: item_text += ' ' + suffix menuitem = RadioMenuItem(group=group, label=item_text) group = menuitem if pref_value and value == pref_value: menuitem.set_active(True) if callback: menuitem.connect('toggled', callback) menu.append(menuitem) if show_notset: menuitem = RadioMenuItem(group=group, label=notset_label) menuitem.set_name('unlimited') if pref_value and pref_value < notset_lessthan: menuitem.set_active(True) menuitem.connect('toggled', callback) menu.append(menuitem) if show_other: menuitem = SeparatorMenuItem() menu.append(menuitem) menuitem = MenuItem(_('Other...')) menuitem.set_name('other') menuitem.connect('activate', callback) menu.append(menuitem) return menu