pygtk托盘图标程序中的CheckMenuItem问题
我有以下 python (pygtk) 程序。当我将鼠标移到显示的菜单上时,如果单击托盘中的项目,复选框将被选中并立即再次取消选中。我使用的是 Ubuntu 10.10 或 11.04。
#!/usr/bin/python
import gtk
import glib
import subprocess
import time
import sys
class StatusIcon:
def __init__(self):
self.statusicon = gtk.StatusIcon()
self.statusicon.set_from_stock(gtk.STOCK_HOME)
self.statusicon.connect("popup-menu", self.right_click_event)
def right_click_event(self, icon, button, time):
"""
We show a menu
"""
menu = gtk.Menu()
submenu = gtk.Menu()
menuitem = gtk.MenuItem("1")
submenu.append(menuitem)
menuitem = gtk.MenuItem("2")
submenu.append(menuitem)
lst = ["a","b","c"]
for item in lst:
newmenuitem = gtk.CheckMenuItem(str(item))
newmenuitem.set_submenu(submenu)
menu.append(newmenuitem)
# Now add all other menu stuff
menu.append(gtk.SeparatorMenuItem())
menuexit = gtk.CheckMenuItem("exit")
menuexit.connect("button-press-event", self.exit)
menu.append(menuexit)
# Show the menu
menu.show_all()
menu.popup(None, None, gtk.status_icon_position_menu, button, time, self.statusicon)
def exit(self, widget, event):
"""
Menu exit pressed
"""
if event.button == 1: #LEFT
print "terminate"
gtk.main_quit()
StatusIcon()
gtk.main()
I have the following python (pygtk) program. When I move the mouse over the menu displayed, if clicked on the item in the tray the check boxes become checked and immediately unchecked again. I'm using Ubuntu 10.10 or 11.04.
#!/usr/bin/python
import gtk
import glib
import subprocess
import time
import sys
class StatusIcon:
def __init__(self):
self.statusicon = gtk.StatusIcon()
self.statusicon.set_from_stock(gtk.STOCK_HOME)
self.statusicon.connect("popup-menu", self.right_click_event)
def right_click_event(self, icon, button, time):
"""
We show a menu
"""
menu = gtk.Menu()
submenu = gtk.Menu()
menuitem = gtk.MenuItem("1")
submenu.append(menuitem)
menuitem = gtk.MenuItem("2")
submenu.append(menuitem)
lst = ["a","b","c"]
for item in lst:
newmenuitem = gtk.CheckMenuItem(str(item))
newmenuitem.set_submenu(submenu)
menu.append(newmenuitem)
# Now add all other menu stuff
menu.append(gtk.SeparatorMenuItem())
menuexit = gtk.CheckMenuItem("exit")
menuexit.connect("button-press-event", self.exit)
menu.append(menuexit)
# Show the menu
menu.show_all()
menu.popup(None, None, gtk.status_icon_position_menu, button, time, self.statusicon)
def exit(self, widget, event):
"""
Menu exit pressed
"""
if event.button == 1: #LEFT
print "terminate"
gtk.main_quit()
StatusIcon()
gtk.main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
子菜单在出现时“切换”复选框的状态。您可以通过处理复选框菜单项的切换信号来对抗这种行为。
The submenu is "toggling" the state of the checkbox when it appears. You can fight this behavior by handling the toggle signal of the checkbox menuitem.