Skip to content

Instantly share code, notes, and snippets.

@pklaus
Created February 15, 2010 20:36
Show Gist options
  • Save pklaus/304963 to your computer and use it in GitHub Desktop.
Save pklaus/304963 to your computer and use it in GitHub Desktop.
StatusIcon – A Simple Tray Icon Application Using PyGTK
#!/usr/bin/env python
# found on <http://files.majorsilence.com/rubbish/pygtk-book/pygtk-notebook-html/pygtk-notebook-latest.html#SECTION00430000000000000000>
# simple example of a tray icon application using PyGTK
import gtk
def message(data=None):
"Function to display messages to the user."
msg=gtk.MessageDialog(None, gtk.DIALOG_MODAL,
gtk.MESSAGE_INFO, gtk.BUTTONS_OK, data)
msg.run()
msg.destroy()
def open_app(data=None):
message(data)
def close_app(data=None):
message(data)
gtk.main_quit()
def make_menu(event_button, event_time, data=None):
menu = gtk.Menu()
open_item = gtk.MenuItem("Open App")
close_item = gtk.MenuItem("Close App")
#Append the menu items
menu.append(open_item)
menu.append(close_item)
#add callbacks
open_item.connect_object("activate", open_app, "Open App")
close_item.connect_object("activate", close_app, "Close App")
#Show the menu items
open_item.show()
close_item.show()
#Popup the menu
menu.popup(None, None, None, event_button, event_time)
def on_right_click(data, event_button, event_time):
make_menu(event_button, event_time)
def on_left_click(event):
message("Status Icon Left Clicked")
if __name__ == '__main__':
icon = gtk.status_icon_new_from_stock(gtk.STOCK_ABOUT)
icon.connect('popup-menu', on_right_click)
icon.connect('activate', on_left_click)
gtk.main()
@wallacesilva
Copy link

thanks for this code man, you saved me 👍

@gevasiliou
Copy link

Poping a tray icon menu on right click and perform a simple action on tray icon left click, works nice with your code.
But when you need to pop a menu on tray icon LEFT mouse click (combination of "activate" and menu.popup ), this becomes a bit tricky,

Using parts of your code and of other people codes, i have made two working examples:
https://github.com/gevasiliou/PythonTests/blob/master/TrayLeftClickMenu.py (combine menu.popup with left click "activate" action)
https://github.com/gevasiliou/PythonTests/blob/master/TrayAllClicksMenu.py (pops up the same menu in both left , right and middle click using "button-press-event")

I'm just posting this info here for future reference of anyone looks similar code to popup the default tray icon menu using any mouse click.

G.V

@jeanCarloMachado
Copy link

This is python 2 only, right?

@giacomomarchioro
Copy link

I've got this error:

AttributeError: When using gi.repository you must not import static modules like "gobject". Please change all occurrences of "import gobject" to "from gi.repository import GObject". See: https://bugzilla.gnome.org/show_bug.cgi?id=709183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment