我的选择菜单的一部分是' recents'菜单,包含5个最近选择的菜单项。因此,每当用户从菜单中选择一个项目时,它应该更新菜单的这一部分。
为了做到这一点,我需要用新的菜单项替换这些菜单项。但显然只是保留对它们的引用并将其指向一个新对象是不够的。菜单不会即时重建。
代码:
def rebuild_recents_menu(self):
counter = 0
for item in self._build_recents_menu():
self.recent_group['base_group'][counter] = item
counter += 1
GLib.idle_add(self.asset_menu.show_all) # self.assets_menu is a reference to the menu to which the submenu is attached
def _build_recents_menu(self):
recent_group = {
'base_group': [],
'subgroup_quotes': [],
'subgroup_exchanges': []
}
for i in range(0,5):
if i > len(self.coin.settings.get('recent'))-1:
base_item = self.create_base_item(None, recent_group, hidden = True)
else:
base = self.coin.settings.get('recent')[i]
base_item = self.create_base_item(base, recent_group)
yield base_item
self.recent_group = recent_group
def create_base_item(self, base, group, hidden = False):
if hidden:
base_item = Gtk.RadioMenuItem.new_with_label(group.get('base_group'), 'hidden')
base_item.hide()
else:
base_item = Gtk.RadioMenuItem.new_with_label(group.get('base_group'), base)
base_item.set_submenu(self._menu_quotes(base, group.get('subgroup_quotes'), group.get('subgroup_exchanges')))
group.get('base_group').append(base_item)
if self.exchange.asset_pair.get('base') == base:
base_item.set_active(True)
base_item.connect('toggled', self._handle_toggle, base)
return base_item