问题描述
我在wxPython中进行了设置,但是现在我要为GTK3/PyGObject重写它,而我在这里有些迷失.基本上,我的代码扫描目录,并将文件名添加到菜单项.我无法在PyGObject中使用它.这是当前代码:
I had this set up in wxPython, but now I'm rewriting it for GTK3/PyGObject, and I'm kind of lost here. Basically, my code scans through a directory, and adds the names of files to a menu item. I can't get it to work in PyGObject. Here is the current code:
# 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,它返回以下错误:
I've tried various profileMenu.* ideas. The first one I tried was set_submenu, which returns the following error:
[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?
The current one just says set_menu_item doesn't exist, so obviously that won't work. What should I try to do to fix this? I'm completely lost. I know why set_submenu doesn't work, I don't know how to fix it. How would I make a filename a Gtk.Widget?
推荐答案
您正在寻找的是append()
.而且,常见的层次结构是:
What you're looking for is append()
. Also, the common hierarchy is:
- GtkMenuBar
- GtkMenuItem
- GtkMenu
- GtkMenuItem
- GtkImageMenuItem
- GtkCheckMenuItem
- GtkRadioMenuItem
- GtkSeparatorMenuItem
- GtkMenuBar
- GtkMenuItem
- GtkMenu
- GtkMenuItem
- GtkImageMenuItem
- GtkCheckMenuItem
- GtkRadioMenuItem
- GtkSeparatorMenuItem
您可以在此处找到完整的示例:
You can find a full example here:
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()
这篇关于以编程方式将新菜单项添加到菜单项PyGObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- GtkMenu
- GtkMenuItem
- GtkMenu
- GtkMenuItem