這是一個遲到的迴應,但我只是發佈這個以防別人曾經尋找左鍵點擊控制gtkstatusicon。
直接選擇是
statusicon.connect("activate", left_button_click)
這是托盤圖標彈出菜單左鍵點擊,而不是(普通)右擊工作的樣本。
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class TrayIcon(gtk.StatusIcon):
def __init__(self):
gtk.StatusIcon.__init__(self)
self.set_from_icon_name('help-about')
self.set_has_tooltip(True)
self.set_visible(True)
self.connect("activate", self.on_click)
def greetme(self,data=None):
msg=gtk.MessageDialog(None, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Greetings")
msg.run()
msg.destroy()
def on_click(self,data):
event=gtk.get_current_event()
btn=event.button #this gets the button value of gtk event.
time=gtk.get_current_event_time() # required by menu popup.
menu = gtk.Menu()
menu_item1 = gtk.MenuItem("First Entry")
menu.append(menu_item1)
menu_item1.connect("activate", self.greetme)
menu_item2 = gtk.MenuItem("Quit")
menu.append(menu_item2)
menu_item2.connect("activate", gtk.main_quit)
menu.show_all()
menu.popup(None, None, None, btn, time)
#button can be hardcoded (i.e 1) but time must be correct.
if __name__ == '__main__':
tray = TrayIcon()
gtk.main()
而且,有這樣的選擇:
statusicon.connect("button-press-event", button_click)
婁示例代碼提出同樣的彈出式菜單中gtktrayicon在右和左擊。
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class TrayIcon(gtk.StatusIcon):
def __init__(self):
gtk.StatusIcon.__init__(self)
self.set_from_icon_name('help-about')
self.set_has_tooltip(True)
self.set_visible(True)
self.connect("button-press-event", self.on_click)
def greetme(self,data=None):
msg=gtk.MessageDialog(None, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Greetings")
msg.run()
msg.destroy()
def on_click(self,data,event):
#event in this case is sent by the status icon connect.
btn=event.button
#By controlling this event.button value (1-2-3 for left-middle-right click) you can call other functions.
time=gtk.get_current_event_time() # required by the popup.
menu = gtk.Menu()
menu_item1 = gtk.MenuItem("First Entry")
menu.append(menu_item1)
menu_item1.connect("activate", self.greetme)
menu_item2 = gtk.MenuItem("Quit")
menu.append(menu_item2)
menu_item2.connect("activate", gtk.main_quit)
menu.show_all()
menu.popup(None, None, None, btn, time)
if __name__ == '__main__':
tray = TrayIcon()
gtk.main()
希望高於代碼有助於。 George V.
我需要在python中使用PyGObject(PyGtk的後繼者)來完成此操作。 – Rainbow
@Rainbow它幾乎是一樣的,因爲PyGObject是Gtk + c庫的綁定。 – erick2red