【问题标题】:Programatically add new menu items to menu item PyGObject?以编程方式将新菜单项添加到菜单项 PyGObject?
【发布时间】:2014-02-05 16:25:34
【问题描述】:

我在 wxPython 中设置了这个,但现在我正在为 GTK3/PyGObject 重写它,我有点迷失在这里。基本上,我的代码扫描目录,并将文件名添加到菜单项。我无法让它在 PyGObject 中工作。这是当前代码:

# Populate profiles menu
        profileMenu = self.builder.get_object("profilesMenu")
        profiles = os.listdir("/etc/netctl/")
        # Iterate through profiles directory, and add to "Profiles" Menu #
        for i in profiles:
            if os.path.isfile("/etc/netctl/" + i):
                profile = profileMenu.set_menu_item(i)

我尝试了各种 profileMenu.* 想法。我尝试的第一个是set_submenu,它返回以下错误:

[cody@cody-arch NetGUI]$ sudo python main.py 
Traceback (most recent call last):
  File "main.py", line 466, in <module>
    netgui()
  File "main.py", line 70, in __init__
    self.InitUI()
  File "main.py", line 140, in InitUI
    profile = profileMenu.set_submenu(i)   
TypeError: argument submenu: Expected Gtk.Widget, but got str

当前的只是说 set_menu_item 不存在,所以显然这行不通。我应该怎么做才能解决这个问题?我完全迷路了。我知道为什么 set_submenu 不起作用,我不知道如何解决它。如何将文件名设为 Gtk.Widget?

【问题讨论】:

    标签: python-3.x gtk3 pygobject


    【解决方案1】:

    您正在寻找的是append()。此外,常见的层次结构是:

    • GtkMenuBar
      • GtkMenuItem
        • GtkMenu
          • GtkMenuItem
          • GtkImageMenuItem
          • GtkCheckMenuItem
          • GtkRadioMenuItem
          • GtkSeparatorMenuItem

    您可以在此处找到完整示例:

    https://gist.github.com/carlos-jenkins/8851942

    代码的相关部分是:

        content = [
            'Path to file 1',
            'Path to file 2',
            'Path to file 3',
            'Path to file 4',
        ]
    
        # Create menu
        menu = Gtk.MenuItem(label='My Menu {}'.format(num))
        menu.set_submenu(Gtk.Menu())
    
        for p in content:
            menu.get_submenu().append(Gtk.MenuItem(label=p))
    
        self.menubar.append(menu)
        menu.show_all()
    

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 2013-10-29
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      相关资源
      最近更新 更多