Simple Gnome Panel Applet in Python

前端 未结 2 1222
一个人的身影
一个人的身影 2020-12-28 08:25

When I\'m on the train to work I connect my netbook to my Nexus One\'s wifi hotspot. As I go through a tunnel my phone obviously loses it\'s 3G connection and takes a while

2条回答
  •  囚心锁ツ
    2020-12-28 09:14

    For future reference, a really nice guide on how to build indicators for Gnome3: http://candidtim.github.io/appindicator/2014/09/13/ubuntu-appindicator-step-by-step.html

    Complete source code:

    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, '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("Joke", fetch_joke(), None).show()
    
    def quit(_):
        notify.uninit()
        gtk.main_quit()
    
    if __name__ == "__main__":
        signal.signal(signal.SIGINT, signal.SIG_DFL)
        main()
    

提交回复
热议问题