Created
June 23, 2020 16:49
-
-
Save sarnold/6fadb4b0dc438956c01fb83f6d664c8e to your computer and use it in GitHub Desktop.
simple pygobject gtk3 app indicator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code was adapted from 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 gi | |
import signal | |
import json | |
import xmltodict | |
import urllib.request | |
gi.require_version('Gtk', '3.0') | |
gi.require_version('AppIndicator3', '0.1') | |
gi.require_version('Notify', '0.7') | |
from gi.repository import Gtk as gtk | |
from gi.repository import AppIndicator3 as appindicator | |
from gi.repository import Notify as notify | |
APPINDICATOR_ID = 'cndbappindicator' | |
def main(): | |
indicator = appindicator.Indicator.new( | |
APPINDICATOR_ID, | |
"network-idle-symbolic", | |
appindicator.IndicatorCategory.APPLICATION_STATUS) | |
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(label='Joke') | |
item_joke.connect('activate', joke) | |
menu.append(item_joke) | |
item_geoip = gtk.MenuItem(label='Geoip') | |
item_geoip.connect('activate', geoip) | |
menu.append(item_geoip) | |
item_quit = gtk.MenuItem(label='Quit') | |
item_quit.connect('activate', quit) | |
menu.append(item_quit) | |
menu.show_all() | |
return menu | |
def fetch_joke(): | |
request = urllib.request.Request('http://api.icndb.com/jokes/random?limitTo=[nerdy]') | |
response = urllib.request.urlopen(request) | |
joke = json.loads(response.read())['value']['joke'] | |
return joke | |
def joke(_): | |
notify.Notification.new("Joke", fetch_joke(), None).show() | |
def fetch_geoip(): | |
request = urllib.request.Request('https://geoip.ubuntu.com/lookup') | |
response = urllib.request.urlopen(request) | |
payload = xmltodict.parse(response.read()) | |
return json.dumps(payload, indent=4, separators=(',', ': ')) | |
def geoip(_): | |
notify.Notification.new("Geoip", fetch_geoip(), None).show() | |
def quit(_): | |
notify.uninit() | |
gtk.main_quit() | |
if __name__ == "__main__": | |
signal.signal(signal.SIGINT, signal.SIG_DFL) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run this using default python3:
External python deps: