From 1ac4a4521c7f2b86afad4f847ed39acf7e9ae7e7 Mon Sep 17 00:00:00 2001 From: Balthasar Reuter Date: Wed, 5 Oct 2016 23:38:48 +0200 Subject: [PATCH] Added a simple Python GUI to set date and time --- events.py | 2 +- gui.py | 34 +++++++++++++++-- photobooth.py | 6 +-- photobooth.sh | 2 +- set-time.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 9 deletions(-) create mode 100755 set-time.py diff --git a/events.py b/events.py index 88b3e39..4bfe035 100644 --- a/events.py +++ b/events.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# Created by br@re-web.eu, 2015 +# Created by br _at_ re-web _dot_ eu, 2015 try: import RPi.GPIO as GPIO diff --git a/gui.py b/gui.py index 07ca85e..4468e9d 100644 --- a/gui.py +++ b/gui.py @@ -1,9 +1,9 @@ #!/usr/bin/env python -# Created by br@re-web.eu, 2015 +# Created by br _at_ re-web _dot_ eu, 2015 from __future__ import division -import pygame +import pygame try: import pygame.fastevent as EventModule @@ -18,7 +18,7 @@ class GuiException(Exception): class GUI_PyGame: """A GUI class using PyGame""" - def __init__(self, name, size): + def __init__(self, name, size, hide_mouse=True): # Call init routines pygame.init() if hasattr(EventModule, 'init'): @@ -28,7 +28,8 @@ class GUI_PyGame: pygame.display.set_caption(name) # Hide mouse cursor - pygame.mouse.set_cursor(*pygame.cursors.load_xbm('transparent.xbm','transparent.msk')) + if hide_mouse: + pygame.mouse.set_cursor(*pygame.cursors.load_xbm('transparent.xbm','transparent.msk')) # Store screen and size self.size = size @@ -87,6 +88,31 @@ class GUI_PyGame: self.surface_list.append((rendered_text, (0,0))) + def show_button(self, text, pos, size=(0,0), color=(230,230,230), bg=(0,0,0), transparency=True, outline=(230,230,230)): + # Choose font + font = pygame.font.Font(None, 72) + text_size = font.size(text) + if size == (0,0): + size = (text_size[0] + 4, text_size[1] + 4) + offset = ( (size[0] - text_size[0]) // 2, (size[1] - text_size[1]) // 2 ) + + # Create Surface object and fill it with the given background + surface = pygame.Surface(self.size) + surface.fill(bg) + + # Render text + rendered_text = font.render(text, 1, color) + surface.blit(rendered_text, pos) + + # Render outline + pygame.draw.rect(surface, outline, (pos[0]-offset[0], pos[1]-offset[0], size[0], size[1]), 1) + + # Make background color transparent + if transparency: + surface.set_colorkey(bg) + + self.surface_list.append((surface, (0,0))) + def wrap_text(self, msg, font, size): final_lines = [] # resulting wrapped text requested_lines = msg.splitlines() # wrap input along line breaks diff --git a/photobooth.py b/photobooth.py index bfba0ca..1f05ed9 100755 --- a/photobooth.py +++ b/photobooth.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# Created by br@re-web.eu, 2015 +# Created by br _at_ re-web _dot_ eu, 2015-2016 import os from datetime import datetime @@ -10,8 +10,8 @@ from time import sleep, clock from PIL import Image from gui import GUI_PyGame as GuiModule -# from camera import CameraException, Camera_cv as CameraModule -from camera import CameraException, Camera_gPhoto as CameraModule +from camera import CameraException, Camera_cv as CameraModule +#from camera import CameraException, Camera_gPhoto as CameraModule from slideshow import Slideshow from events import Rpi_GPIO as GPIO diff --git a/photobooth.sh b/photobooth.sh index c62703e..d32b850 100755 --- a/photobooth.sh +++ b/photobooth.sh @@ -5,7 +5,7 @@ PHOTOBOOTH_DIR=/home/pi/photobooth cd "${PHOTOBOOTH_DIR}" if [[ $1 == "set-time" ]]; then - sudo gnome-control-center datetime + sudo python set-time.py fi sudo python photobooth.py >>photobooth.log 2>>photobooth.err diff --git a/set-time.py b/set-time.py new file mode 100755 index 0000000..4b40306 --- /dev/null +++ b/set-time.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python +# Created by br _at_ re-web _dot_ eu, 2016 + +from gui import GUI_PyGame as GuiModule +from time import sleep + +import subprocess + +# Screen size +display_size = (1024, 600) + +# Button size +button_size = (70, 70) + +date_digits = ['D', 'D', 'M', 'M', 'Y', 'Y', 'Y', 'Y'] # DD-MM-YYYY +time_digits = ['H', 'H', 'M', 'M'] # HH-MM + +numpad = { '1': (100, 100), '2': (200, 100), '3': (300, 100), + '4': (100, 200), '5': (200, 200), '6': (300, 200), + '7': (100, 300), '8': (200, 300), '9': (300, 300), + '0': (200, 400) } + +################# +### Functions ### +################# + +def check_and_handle_events(display, digit): + r, e = display.check_for_event() + while r: + handle_event(e, digit) + r, e = display.check_for_event() + +def handle_event(event, digit, digits, numpad): + # mouseclick + if event.type == 2 and event.value[0] == 1: + print(event.value[1]) + for num, pos in numpad.items(): + if (event.value[1][0] > pos[0] and + event.value[1][0] < pos[0] + button_size[0] and + event.value[1][1] > pos[1] and + event.value[1][1] < pos[1] + button_size[1]): + digits[digit] = num + return True + return False + +def show_numpad(display, numpad, button_size): + for num, pos in numpad.items(): + display.show_button(num, pos, button_size) + +def show_digits(display, digits, button_size): + for i in range(len(digits)): + display.show_button(digits[i], (400 + i * (button_size[0] + 5), 200), button_size, outline=(0,0,0)) + +def main(): + display = GuiModule('set-time', display_size, hide_mouse=False) + + for digit in range(len(date_digits)): + display.clear() + + show_numpad(display, numpad, button_size) + display.show_button('Date:', (400, 100), outline=(0,0,0)) + show_digits(display, date_digits, button_size) + + display.apply() + + digit_done = False + while not digit_done: + event = display.wait_for_event() + digit_done = handle_event(event, digit, date_digits, numpad) + + for digit in range(len(time_digits)): + display.clear() + + show_numpad(display, numpad, button_size) + display.show_button('Time:', (400, 100), outline=(0,0,0)) + show_digits(display, time_digits, button_size) + + display.apply() + + digit_done = False + while not digit_done: + event = display.wait_for_event() + digit_done = handle_event(event, digit, time_digits, numpad) + + # YYYY-MM-DD HH:mm + date_str = ( '"' + + date_digits[4] + date_digits[5] + date_digits[6] + date_digits[7] + '-' + + date_digits[2] + date_digits[3] + '-' + + date_digits[0] + date_digits[1] + ' ' + + time_digits[0] + time_digits[1] + ':' + time_digits[2] + time_digits[3] + + '"' ) + + subprocess.check_call(['date', '-s', date_str]) + + display.teardown() + return 0 + +if __name__ == "__main__": + exit(main()) + \ No newline at end of file