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()
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.
class StatusIcon:
...
def callback(self, widget, data=None):
widget.set_active(False)
...
for item in lst:
newmenuitem = gtk.CheckMenuItem(str(item))
newmenuitem.connect("toggled", self.callback, "")
精彩评论