pygtk trayicon程序中的CheckMenuItem问题

时间:2011-06-17 11:24:21

标签: python menu checkbox pygtk trayicon

我有以下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()

1 个答案:

答案 0 :(得分:2)

子菜单显示“切换”复选框的状态。您可以通过处理复选框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, "")