PyGTK Status Icons
This is a simple cook book style approach to status icons. Page one shows how to add a status icon to the gnome notification area. Page two adds left clicking on the status icon. Page three shows using signals and callbacks to perform actions when menu options are clicked.
First you will want an icon. Gtk comes with many stock icons. For this example gtk.STOCK_ABOUT will be used:
icon = gtk.status_icon_new_from_stock(gtk.STOCK_ABOUT)
Next, when the status icon is clicked something needs to happen. For this add a call back for right clicks:
icon.connect('popup-menu', on_right_click)
So now there is a icon on the notification area and when it is right clicked, the function on_right_clicked is called. In right click the menu will be set up or it should call another function to set up the menu.
The function on_right_click passes the data on to make_menu. Make menu creates a new menu and appends a menu item to the newly created menu. Then sets the menu item to show. The very last part that takes place is to popup the menu.
For more specific information about menus and status icon functions visit their referernce pages. The address are listed below the following code sample.
#!/usr/bin/env python
import gtk
def make_menu(event_button, event_time, data=None):
#Create a menu and add a menu item.
menu = gtk.Menu()
open_item = gtk.MenuItem("Open App")
#Append and show a menu item
menu.append(open_item)
open_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)
if __name__ == '__main__':
#set the icon used for the status icon.
icon = gtk.status_icon_new_from_stock(gtk.STOCK_ABOUT)
#set callback on right click to on_right_click
icon.connect('popup-menu', on_right_click)
gtk.main()
A list of stock items that can be used as an icon can be found at http://pygtk.org/docs/pygtk/gtk-stock-items.html.
More information about the PyGTK status icon can be found at the reference page http://pygtk.org/docs/pygtk/class-gtkstatusicon.html.
More information about the PyGTK menus can be found at the reference page http://pygtk.org/docs/pygtk/class-gtkmenu.html.
The only difference between the code on this page and the first page is that left clicking can also be used now. At the bottom of the source the following line as been added.
icon.connect('activate', on_left_click)
This line adds a callback so that when the status icon is left clicked it will call the on_left_clicked function.
The menu item close_item has also been added in the make_menu function. Run the code and observe the output.
#!/usr/bin/env python
import gtk
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)
#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):
print("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()
This final example shows how a status icon can be used to have events happen on left clicks or when a menu item is clicked.
The menu items in make_menu have had callback functions added.
The make_menu callbacks that were added are:
open_item.connect_object("activate", open_app, "Open App")
close_item.connect_object("activate", close_app, "Close App")
So if "Open App" is clicked the function open_app is called and if "Close App" is clicked the function close_app is called.
Just like in the first example an icon is choosen. Then right click and left click callbacks are added to the status icon.
The function message has also been added but is insignificant and has no function towards using the status icon. It is only used to display output on clicks as an example.
Run the code and see what events happen when left clicks are performed and when menu items are selected.
#!/usr/bin/env python
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, icon):
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)
def on_right_click(icon, event_button, event_time):
make_menu(event_button, event_time, icon)
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()