Created
March 25, 2014 19:19
-
-
Save petrblahos/9769152 to your computer and use it in GitHub Desktop.
Small python gtk sample.
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
from gi.repository import Gtk, GLib | |
class LedControl(Gtk.Window): | |
""" | |
A simple app that changes images according to the button status. | |
""" | |
def __init__(self): | |
Gtk.Window.__init__(self, title="LED Control") | |
self.set_border_width(6) | |
self.set_default_size(200, 250) | |
self.blink_counter = 0 | |
GLib.timeout_add(500, self.blink_timer, None) | |
okno = Gtk.Table(5, 2, True) | |
self.add(okno) | |
self.leds = [] | |
for i in range(4): | |
button = Gtk.ToggleButton("LED %s" % i) | |
button.connect("toggled", self.led_button_handler, str(i)) | |
button.set_border_width(2) | |
img = Gtk.Image() | |
img.set_from_file("LED_off.png") | |
okno.attach(button, 0, 1, i, i + 1) | |
okno.attach(img, 1, 2, i, i + 1) | |
self.leds.append(img) | |
self.blinking_on = Gtk.ToggleButton("Blikanie") | |
self.blinking_on.set_border_width(2) | |
okno.attach(self.blinking_on, 0, 1, 4, 5) | |
def led_button_handler(self, button, name): | |
""" | |
When a led-related button was pressed. | |
""" | |
self.leds[int(name)].set_from_file( | |
"LED_on.png" if button.get_active() else "LED_off.png", | |
) | |
def blink_timer(self, dummy_data): | |
""" | |
Timer to control blinking. | |
""" | |
self.blink_counter += 1 | |
if self.blinking_on.get_active(): | |
print "Blinking [%s]" % self.blink_counter | |
for led in range(4): | |
self.leds[led].set_from_file( | |
"LED_on.png" if self.blink_counter % 2 else "LED_off.png", | |
) | |
else: | |
print "Not blinking [%s]" % self.blink_counter | |
return True | |
if "__main__" == __name__: | |
app = LedControl() | |
app.connect("delete-event", Gtk.main_quit) | |
app.show_all() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment