Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Ubuntu AppIndicator to show Chuck Norris jokes
# This code is an example for a tutorial on Ubuntu Unity/Gnome AppIndicators:
# http://candidtim.github.io/appindicator/2014/09/13/ubuntu-appindicator-step-by-step.html
import os
import signal
import json
from urllib2 import Request, urlopen, URLError
from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Notify as notify
APPINDICATOR_ID = 'myappindicator'
def main():
indicator = appindicator.Indicator.new(APPINDICATOR_ID, os.path.abspath('sample_icon.svg'), appindicator.IndicatorCategory.SYSTEM_SERVICES)
indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
indicator.set_menu(build_menu())
notify.init(APPINDICATOR_ID)
gtk.main()
def build_menu():
menu = gtk.Menu()
item_joke = gtk.MenuItem('Joke')
item_joke.connect('activate', joke)
menu.append(item_joke)
item_quit = gtk.MenuItem('Quit')
item_quit.connect('activate', quit)
menu.append(item_quit)
menu.show_all()
return menu
def fetch_joke():
request = Request('http://api.icndb.com/jokes/random?limitTo=[nerdy]')
response = urlopen(request)
joke = json.loads(response.read())['value']['joke']
return joke
def joke(_):
notify.Notification.new("<b>Joke</b>", fetch_joke(), None).show()
def quit(_):
notify.uninit()
gtk.main_quit()
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal.SIG_DFL)
main()

This got released the same day as my chuck jokes application haha

Zibri commented Dec 18, 2016 edited

python2 myappindicator_v5.py

myappindicator_v5.py:10: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded.
from gi.repository import Gtk as gtk
myappindicator_v5.py:11: PyGIWarning: AppIndicator3 was imported without specifying a version first. Use gi.require_version('AppIndicator3', '0.1') before import to ensure that the right version gets loaded.
from gi.repository import AppIndicator3 as appindicator
myappindicator_v5.py:12: PyGIWarning: Notify was imported without specifying a version first. Use gi.require_version('Notify', '0.7') before import to ensure that the right version gets loaded.
from gi.repository import Notify as notify

Zibri commented Dec 18, 2016

python3 myappindicator_v5.py

Traceback (most recent call last):
File "myappindicator_v5.py", line 8, in
from urllib2 import Request, urlopen, URLError
ImportError: No module named 'urllib2'

osh2ep commented Mar 19, 2017

@Zibri: use Requests instead.

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