Skip to content

Instantly share code, notes, and snippets.

Created January 12, 2016 05:38
Show Gist options
  • Save tdack/b6cda64b302f67922493 to your computer and use it in GitHub Desktop.
Save tdack/b6cda64b302f67922493 to your computer and use it in GitHub Desktop.
Kivy custom properties on a widget
import kivy
from import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from import NumericProperty, ObjectProperty
import RPi.GPIO as GPIO
Base class for a GPIO based button
class GPIOButton(Button):
pin = NumericProperty(None)
direction = NumericProperty(None)
pull_up_down = NumericProperty(None, allownone=True)
value = ObjectProperty(None)
def __init__(self, **kwargs):
super(GPIOButton, self).__init__(**kwargs)
# Setup GPIO pin based on .kv properties
if is None:
# pin not set, so don't do any configuring
# Below never gets executed as is always None in __init__()
if self.direction == GPIO.OUT:
print("setting up {}".format(
GPIO.setup(, self.direction, pull_up_down=self.pull_up_down)
GPIO.setup(, self.direction)
if self.value is not None:
GPIO.output(, self.value)
""" Button that will toggle based on GPIO input """
class GPIOInputButton(GPIOButton):
""" Button that will change a GPIO output """
class GPIOPressButton(GPIOButton, Button):
class Monitor(Widget):
def init_GPIO(self):
# Set up GPIO
class MonitorApp(App):
def build(self):
monitor = Monitor()
return monitor
if __name__ == '__main__':
#:kivy 1.0
#:import GPIO RPi.GPIO
cols: 5
padding: 30
spacing: 30
size: self.parent.size
row_default_height: 150
id: btn_input
text: 'Input'
pin: 22
direction: GPIO.IN
pull_up_down: GPIO.PUD_UP
id: btn_beep
text: 'BEEP!'
pin: 17
direction: GPIO.OUT
value: GPIO.LOW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment