Skip to content

Instantly share code, notes, and snippets.

@jmarroyave
Forked from candidtim/myappindicator_v5.py
Last active January 11, 2019 22:34
Show Gist options
  • Save jmarroyave/a24bf173092a3b0943402f6554a2094d to your computer and use it in GitHub Desktop.
Save jmarroyave/a24bf173092a3b0943402f6554a2094d to your computer and use it in GitHub Desktop.
Ubuntu AppIndicator to show Chuck Norris jokes - Updated to python 3
# 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 urllib import request
from urllib.error import URLError
from urllib.request import urlopen
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():
with urlopen('http://api.icndb.com/jokes/random?limitTo=[nerdy]') as req:
msg = req.read();
msg = msg.decode('utf-8')
joke = json.loads(msg)['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()
@ToJen
Copy link

ToJen commented Dec 22, 2017

hey, how did you work around this on python3

user:my-indicator$ python test.py
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    from gi.repository import Gtk as gtk
  File "/home/user/anaconda3/lib/python3.5/site-packages/gi/__init__.py", line 39
    print url
            ^
SyntaxError: Missing parentheses in call to 'print'

@TheRochVoices
Copy link

In python3, "print" is a function, not a statement. Thus instead of print url type print(url).

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