发表时间:2008-05-20
最后修改:2009-04-25
python system tray icon
一个简单的,一个完整的。
1 #!/usr/bin/env python
2 #-*- encoding:utf-8 -*-
3
4 import gtk
5
6 def on_activate(data):
7 print 'active'
8
9 def on_popup_menu(status, button, time):
10 print 'popup menu'
11
12 tr=gtk.StatusIcon()
13 tr.set_from_file("1.gif")
14 tr.set_tooltip('test tray in python')
15 tr.set_visible(True)
16 tr.connect('activate', on_activate)
17 tr.connect('popup-menu', on_popup_menu)
18
19 gtk.main()
====================
1 #!/usr/bin/env python
2 #-*- encoding:utf-8 -*-
3
4 import os
5 import gtk
6
7 class MyStatusIcon(gtk.StatusIcon):
8 def __init__(self):
9 gtk.StatusIcon.__init__(self)
10 menu = '''
11 <ui>
12 <menubar name="Menubar">
13 <menu action="Menu">
14 <menuitem action="action1"/>
15 <menuitem action="action2"/>
16 <separator/>
17 <menuitem action="About"/>
18 <separator/>
19 <menuitem action="Quit"/>
20 </menu>
21 </menubar>
22 </ui>
23 '''
24 actions = [
25 ('Menu', None, 'Menu'),
26 ('action1', None, 'action1', None, 'action1 for test', self.on_action),
27 ('action2', None, 'action2', None, 'action2 for test', self.on_action),
28 ('About', gtk.STOCK_ABOUT, 'About', None, 'About', self.on_about),
29 ('Quit', gtk.STOCK_QUIT, 'Quit', None, 'Quit', self.on_quit)
30 ]
31
32 ag = gtk.ActionGroup('Actions')
33 ag.add_actions(actions)
34 self.manager = gtk.UIManager()
35 self.manager.insert_action_group(ag, 0)
36 self.manager.add_ui_from_string(menu)
37 self.menu = self.manager.get_widget('/Menubar/Menu/About').props.parent
38 self.set_from_file('1.gif')
39 self.set_tooltip('kf701 python tray use gtk statusicon')
40 self.set_visible(True)
41 self.connect('activate', self.on_activate)
42 self.connect('popup-menu', self.on_popup_menu)
43
44 def on_activate(self, data):
45 print 'on_activate'
46
47 def on_popup_menu(self, status, button, time):
48 self.menu.popup(None, None, None, button, time)
49
50 def on_action(self, data):
51 print 'on_action'
52
53 def on_about(self, data):
54 dialog = gtk.AboutDialog()
55 dialog.set_name('kf701 python tray test')
56 dialog.set_version('0.1')
57 dialog.set_comments('Use gtk statusicon in python')
58 dialog.set_website('http://kf701.cublog.cn')
59 dialog.run()
60 dialog.destroy()
61
62 def on_quit(self, data):
63 print 'Exit'
64 gtk.main_quit()
65
66 if __name__ == '__main__':
67 MyStatusIcon()
68 gtk.main()