Skip to content

Instantly share code, notes, and snippets.

@eliasdorneles
Last active November 26, 2015 09:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasdorneles/856f9363709b1c89e81e to your computer and use it in GitHub Desktop.
Save eliasdorneles/856f9363709b1c89e81e to your computer and use it in GitHub Desktop.
For Those Times When You Just Want a Button
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
For those times when you just want a button.
Run and notice the new icon in your notifications area.
Inspired by: https://glyph.twistedmatrix.com/2015/07/just-a-button.html
"""
import signal
from collections import namedtuple
from gi.repository import Gtk
from gi.repository import AppIndicator3 as appindicator
# Helper classes here, scroll down to see app code
Action = namedtuple('Action', 'label callback')
class Indicator(object):
"""Indicator wrapper, for a more Pythonic API
"""
allowed_statuses = tuple(s for s in dir(appindicator.IndicatorStatus)
if s.isupper())
def __init__(self, indicator_id, icon_name):
self.indicator = appindicator.Indicator.new(
id=indicator_id,
icon_name=icon_name,
category=appindicator.IndicatorCategory.SYSTEM_SERVICES)
self.status = 'ACTIVE'
@property
def status(self):
return self.indicator.status
@status.setter
def status(self, status):
if status not in self.allowed_statuses:
raise ValueError("Invalid status: %s (allowed: %s)"
% Indicator.allowed_statuses)
indicator_status = getattr(appindicator.IndicatorStatus, status)
self.indicator.set_status(indicator_status)
@property
def menu(self):
return self.indicator.get_menu()
@menu.setter
def menu(self, menu):
self.indicator.set_menu(menu)
def build_menu(actions):
menu = Gtk.Menu()
for action in actions:
button = Gtk.MenuItem(action.label)
button.connect('activate', action.callback)
menu.append(button)
menu.show_all()
return menu
class JustaButtonApp(object):
def __init__(self, *args, **kwargs):
icon = "emblem-art" # pick your icon at: /usr/share/icons/
indicator = Indicator('my-app-id', icon_name=icon)
# define the actions here:
actions = [
Action('Button 1', self.on_button_click),
Action('Button 2', self.on_button_click),
Action('Quit', Gtk.main_quit),
]
indicator.menu = build_menu(actions)
Gtk.main()
# to use more widgets, see docs at: https://python-gtk-3-tutorial.readthedocs.org
def on_button_click(self, button):
print("You've clicked: %s" % button.get_label())
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal.SIG_DFL)
JustaButtonApp()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment