在 python 的 gtk3+ 中,如何获取选定的菜单项或选定的菜单项的索引?

In gtk3+ for python how to get selected menuitem or index of menuitem selected?

def search(self,button):
    print 'search clicked'
    self.popup_menu=Gtk.Menu()
    s=self.search_field.get_text()
    if s:
        href, title, size, seeders, leechers= scrape_test.main() 
        for i in range(len(href)):

            i1 = Gtk.MenuItem(str(i+1)+'. '+title[i]+' '+size[i]+'       '+seeders[i]+' '+leechers[i])
            self.popup_menu.append(i1)
            print ' '
        self.popup_menu.show_all()
        self.popup_menu.popup(None, None, None, None, 0, Gtk.get_current_event_time())

弹出菜单显示了菜单,但我想获取所选菜单项或所选菜单项的索引。我该怎么做?

据我了解,您必须将处理程序连接到每个 MenuItem 的 "activate" 事件。像这样:

def search(self):
    print("search clicked")
    self.popup_menu = Gtk.Menu()
    s = "Some text to test "
    if s:
        for i in range(5):
            i1 = Gtk.MenuItem(s + str(i))
            self.popup_menu.append(i1)
            i1.connect("activate", self.item_activated, i)
            print(' ')
        self.popup_menu.show_all()
        self.popup_menu.popup(None, None, None, None, 0, \
                              Gtk.get_current_event_time())

def item_activated(self, wdg, i):
    print('Item activated ', i)

我已经简化了菜单项的实际文本,因为我没有获取实际文本的功能。