Skip to content

Instantly share code, notes, and snippets.

@ramkumarshankar
Created August 24, 2013 12:19
Show Gist options
  • Save ramkumarshankar/6327804 to your computer and use it in GitHub Desktop.
Save ramkumarshankar/6327804 to your computer and use it in GitHub Desktop.
A simple skeleton Ubuntu indicator applet.
#!/usr/bin/env python
#
# Copyright 2013 Ramkumar Shankar
#
# Author: Ramkumar Shankar <ram.i.am@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import gtk
import appindicator
import pynotify
# Main class for the indicator
# Initialize and set up your methods here
class MyIndicator:
def __init__(self):
# initialization happens here
self.init_notification()
self.ind = appindicator.Indicator("my-indicator",
self.set_idle_icon(),
appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_attention_icon(self.set_attention_icon())
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.menu_setup()
self.ind.set_menu(self.menu)
def set_idle_icon(self):
'''
The idle icon
Keep the file in a folder named 'icons' where this python script resides
and change the filename in the line below
'''
return os.path.dirname(os.path.realpath(__file__)) + os.path.sep + "icons" + os.path.sep + "idle_icon.png"
def set_attention_icon(self):
'''
The attention icon
Keep the file in a folder named 'icons' where this python script resides
and change the filename in the line below
'''
return os.path.dirname(os.path.realpath(__file__)) + os.path.sep + "icons" + os.path.sep + "attention_icon.png"
def init_notification(self):
'''
Create a notification to be displayed when menu items are clicked.
This is just to initialize the object. We don't show anything yet.
'''
pynotify.init("my-indicator")
self.myNotification = pynotify.Notification("something clicked",
"responding to user input")
def menu_setup(self):
'''
Indicator menu items are set up here. Change this as needed.
We will set up a few different items for illustrative purposes
'''
self.menu = gtk.Menu()
self.menu_item = gtk.MenuItem("Normal Item")
self.check_item = gtk.CheckMenuItem("Set Attention")
self.separator_item_1 = gtk.SeparatorMenuItem()
self.radio_menu_group = gtk.RadioMenuItem()
self.radio_item_1 = gtk.RadioMenuItem(self.radio_menu_group, "Radio Item 1")
self.radio_item_2 = gtk.RadioMenuItem(self.radio_menu_group, "Radio Item 2")
self.separator_item_2 = gtk.SeparatorMenuItem()
self.quit_item = gtk.MenuItem("Quit")
# make each item do something
self.menu_item.connect("activate", self.click_menu_item)
self.check_item.connect("toggled", self.toggle_check_item)
self.radio_item_1.connect("activate", self.toggle_radio_item)
self.radio_item_2.connect("activate", self.toggle_radio_item)
self.quit_item.connect("activate", self.quit)
# add each item to the menu
self.menu.append(self.menu_item)
self.menu.append(self.check_item)
self.menu.append(self.separator_item_1)
self.menu.append(self.radio_item_1)
self.menu.append(self.radio_item_2)
self.menu.append(self.separator_item_2)
self.menu.append(self.quit_item)
# show each item
self.menu_item.show()
self.check_item.show()
self.separator_item_1.show()
self.radio_item_1.show()
self.radio_item_2.show()
self.separator_item_2.show()
self.quit_item.show()
def main(self):
# Main loop - starts the indicator
gtk.main()
def click_menu_item(self, widget):
self.myNotification.update("menu item clicked",
"You could do something else here")
self.myNotification.show()
def toggle_check_item(self, widget):
# when checked, toggle attention state too
if widget.active:
self.ind.set_status(appindicator.STATUS_ATTENTION)
self.myNotification.update("check item activated",
"my icon is now blue!")
self.myNotification.show()
else:
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.myNotification.update("check item deactivated",
"the icon is back to white now")
self.myNotification.show()
def toggle_radio_item(self, widget):
self.myNotification.update(widget.get_label() + " selected")
self.myNotification.show()
def quit(self, widget):
sys.exit(0)
if __name__ == "__main__":
ind = MyIndicator()
ind.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment