在python的menuitem中将函数设置为label

2024-04-23 23:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Python(gtk)中的MenuItem中将函数设置为label。每次调用函数时,我都要更改MenuItem的标签。你知道吗

根据文档-https://developer.gnome.org/pygtk/stable/class-gtkmenuitem.html#constructor-gtkmenuitemMenuItem的标签必须是字符串。我怎样才能做到这一点?你知道吗

   item5 = Gtk.MenuItem('Force Quit')
   item5.connect("activate", self.run_command4)
   item6 = Gtk.MenuItem('Sleep')

我想要的是:

   a = os.path.basename(active_window_name)
   item5 = Gtk.MenuItem('Force Quit' + a)

“active\u window\u name”是每次打开菜单时调用的函数的返回已创建。作为如上所述,('Force Quit' + a)不是MenuItem的有效标签。你知道吗

编辑

print(type(a)给出<type 'str'>。你知道吗

另外,代码是Ubuntu budgee applet。你知道吗

在PyCharm中,当我运行applet时有code 0,但是当我添加applet时,它不会加载到面板中。你知道吗

回溯

/usr/bin/python3.7 "/home/christoforos/Documents/Apple Menu New/applemenunew.py"

(applemenunew.py:5061): Gtk-WARNING **: 19:21:17.967: Theme parsing error: gtk.css:4:11: 'height' is not a valid property name

(applemenunew.py:5061): dbind-WARNING **: 19:21:18.114: Couldn't register with accessibility bus: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.

Process finished with exit code 0

任何帮助都将不胜感激。你知道吗


Tags: the函数namepygtknot标签reply