在PyGTK中动态修改/刷新菜单内容 [英] Dynamically modifying/refreshing menu contents in PyGTK

查看:80
本文介绍了在PyGTK中动态修改/刷新菜单内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用PyGTK编写的GUI的菜单中实现最近打开的项目的列表.我像这样初始化菜单:

I am trying to implement a list of recently opened items in the menu of a GUI I am writing with PyGTK. I initialize the menu like so:

self.filemenu = gtk.Menu()

self.init_file_menu()

self.fileitem = gtk.MenuItem("File")
self.fileitem.set_submenu(self.filemenu)
menubar = gtk.MenuBar()
menubar.append(self.fileitem)
outerframe.pack_start(menubar, False, False)

def init_file_menu(self):
    for widget in self.filemenu.get_children():
        self.filemenu.remove(widget)

    openitem = gtk.MenuItem("Open")
    self.filemenu.append(openitem)
    openitem.connect("activate", self.open_file)

    self.filemenu.append(gtk.SeparatorMenuItem())

    for recentitem in self.settings['recentfiles']:
        item = gtk.MenuItem(os.path.basename(recentitem))
        self.filemenu.append(item)
        item.connect("activate", self.open_file, recentitem)
    self.filemenu.show()

self.settings ['recentitems']是在打开文件时修改的双端队列.然后再次调用init_file_menu.我的目标是清空菜单,然后以正确的顺序用正确的项目重新填充菜单.在启动时填充菜单时,此代码可以正常工作.但是由于某些原因,尽管对Menu.remove的调用都可以正常工作并清空菜单,但是append调用不会向其中重新添加项目,因此该菜单保持为空.如果我调用它的get_children方法,我会看到它们都在内部,但是接口尚未更新以反映这一点.如何刷新显示的菜单并使最近的项目列表按预期工作?

self.settings['recentitems'] is a deque that is modified when opening a file; init_file_menu is then called again. My goal is to empty the menu, then repopulate it with the right items in the right order. This code works fine when populating the menu at startup. But for some reason while the calls to Menu.remove all work fine and empty the menu, the append calls do not re-add the items to it and it remains empty. If I call its get_children method, I see that they are all there internally, but the interface has not been updated to reflect this. How do I refresh the displayed menu and make the recent items list work as expected?

我还将接受有关如何使用 RecentChooserMenu 小部件的指导如果这就是我想要的.

I would also accept direction on how to use the RecentChooserMenu widget if that is what I am looking for.

推荐答案

由于gtk.Menu是容器,因此您需要将每个项目添加到菜单后.show(因为您不想调用menu.show_all()显示菜单本身的快捷方式),因此您只需在menu.append(item)

Since gtk.Menu is a container, you need to .show every item after adding it to menu (Because you don't want to call menu.show_all() shortcut which shows the menu itself), so you do item.show() just after menu.append(item)

这篇关于在PyGTK中动态修改/刷新菜单内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆