Skip to content

Instantly share code, notes, and snippets.

@afteroot
Created September 28, 2017 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afteroot/8a115d6957a1dc8d928f17de6996af29 to your computer and use it in GitHub Desktop.
Save afteroot/8a115d6957a1dc8d928f17de6996af29 to your computer and use it in GitHub Desktop.
Applet python
#!/usr/bin/python
# 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
# This one is required, but should already be installed
# For Notify
#sudo apt-get install python-gobject
#sudo apt-get install libnotify-bin
#sudo apt-get install libnotify-dev
#For notifications
# http://www.devdungeon.com/content/desktop-notifications-python-libnotify
import os
import signal
import json
from time import sleep
from urllib2 import Request, urlopen
from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Notify as notify
APPINDICATOR_ID = 'app'
show_notify = True
def quit(_):
notify.uninit()
gtk.main_quit()
def status(widget):
global show_notify
if widget.get_active():
show_notify = True
else:
show_notify = False
#http://candidtim.github.io/appindicator/2014/09/13/ubuntu-appindicator-step-by-step.html
def main():
indicator = appindicator.Indicator.new(APPINDICATOR_ID, os.path.abspath('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():
# Create a menu
main_menu = gtk.Menu()
listMenu = gtk.Menu()
# Sub menu
listItems = gtk.MenuItem("Configuration")
listItems.set_submenu(listMenu)
_joke = gtk.MenuItem("Joke")
_config = gtk.MenuItem("Config")
_config.set_tooltip_text(" Test ")
listMenu.append(_joke)
listMenu.append(_config)
_joke.connect('activate', joke)
_config.connect('activate', win)
_notify = gtk.CheckMenuItem("Notify")
_notify.connect('activate', status)
_notify.set_active(True)
# create exit menu
_quit = gtk.MenuItem("Quit")
_quit.connect("activate", quit)
main_menu.append(listItems)
main_menu.append(_notify)
main_menu.append(_quit)
main_menu.show_all()
return main_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(self):
if show_notify:
notify.Notification.new("<b>Joke</b>", fetch_joke(), None).show()
return True
else:
return False
#http://python-gtk-3-tutorial.readthedocs.io/en/latest/application.html#example
#http://python-gtk-3-tutorial.readthedocs.io/en/latest/button_widgets.html#button
def win(self):
class Handler:
def onDeleteWindow(self, *args):
gtk.main_quit(*args)
def onButtonPressed(self, button):
label.set_text("Hello World")
builder = gtk.Builder()
builder.add_from_file("main.glade")
window = builder.get_object("main_window")
label = builder.get_object("label1")
builder.connect_signals(Handler())
window.show_all()
gtk.main()
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal.SIG_DFL)
main()
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.0"/>
<object class="GtkWindow" id="main_window">
<property name="can_focus">False</property>
<property name="window_position">center-always</property>
<signal name="delete-event" handler="onDeleteWindow" swapped="no"/>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes"></property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">False</property>
<property name="padding">83</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="margin_left">51</property>
<property name="margin_right">51</property>
<property name="margin_bottom">11</property>
<signal name="pressed" handler="onButtonPressed" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[ ] About Dialog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment