#!/usr/bin/env python # Anzeige von Spielständen und Werbung import PySimpleGUI as sg import os from PIL import Image, ImageTk import io import platform #import time # ------------------------------------------------------------------------------ # use PIL to read data of one image # ------------------------------------------------------------------------------ def get_img_data(f, maxsize=(1920, 1080), first=False): """Generate image data using PIL """ img = Image.open(f) img.thumbnail(maxsize, resample = Image.BILINEAR) if first: # tkinter is inactive the first time bio = io.BytesIO() img.save(bio, format="PNG") del img return bio.getvalue() return ImageTk.PhotoImage(img) # ------------------------------------------------------------------------------ def filter(fil_event): code = int(fil_event[fil_event.index(':')+1:]) # nervige Kodierung der Tasten im RaspPi NumPad! # Basisnummer 79 = 7 etc. numPad = ['7', '8', '9', '-', '4', '5', '6', '+', '1', ' ', '3', '0', ','] r = " " if code == 63: r='*' if code>= 79 and code <=91: r = numPad[code-79] return r # ------------------------------------------------------------------------------ # Spielstand zählen # ------------------------------------------------------------------------------ def zaehlen(taste): global z, select z_col = ["black", "black", "black", "black", "black"] color_cursor = "grey20" print(taste) flag = False if taste[0] == '1': select = 3 z_col[3] = color_cursor flag = True elif taste[0] == '3': select = 4 z_col[4] = color_cursor flag = True elif taste[0] == '7': select = 1 z_col[1] = color_cursor flag = True elif taste[0] == '9': select = 2 z_col[2] = color_cursor flag = True elif taste[0] == '+': if z[select] < 99: z[select] = z[select] +1 select = 0 flag = True elif taste[0] == '-': if z[select] > 0: z[select] = z[select] - 1 select = 0 flag = True elif taste[0] == '0': if select == 1 or select == 2: z[1:3] = [0, 0] flag = True select = 0 elif select == 3 or select == 4: z[3:5] = [0, 0] flag = True select = 0 elif taste[0] == 'u': flag = True if flag: window1["-a1-"].update(z[1], z_col[1]) window1["-a2-"].update(z[2], z_col[2]) window1["-a3-"].update(z[3], z_col[3]) window1["-a4-"].update(z[4], z_col[4]) # ------------------------------------------------------------------------------ # Main start # ------------------------------------------------------------------------------ if platform.system() == 'Windows': folder = "C:/tmp/Werbung" elif platform.system() == 'Linux': folder = "/boot/Werbung" else: sg.popup('Unbekanntes Betriebssystem! ') raise SystemExit() # PIL supported image types img_types = (".png", ".jpg", "jpeg", ".tiff", ".bmp") # get list of files in folder flist0 = os.listdir(folder) # create sub list of image files (no sub folders, no wrong file types) fnames = [f for f in flist0 if os.path.isfile( os.path.join(folder, f)) and f.lower().endswith(img_types)] num_files = len(fnames) # number of images found if num_files == 0: sg.popup('Keine Bilder im Ordner Werbung! ') raise SystemExit() del flist0 # no longer needed sg.theme('Black') a1 = sg.Text('0', font=("Arial", 200), text_color = "grey72", justification = "right", size = 2, key = "-a1-") a2 = sg.Text('0', font=("Arial", 200), text_color = "SpringGreen4", justification = "left", size = 2, key = "-a2-") a3 = sg.Text('0', font=("Arial", 400), text_color = "Red", justification = "right", size = 2, key = "-a3-") a4 = sg.Text('0', font=("Arial", 400), text_color = "yellow2", justification = "left", size = 2, key = "-a4-") t1 = sg.Text(':', font=("Arial", 200, "bold"), text_color = "white") t2 = sg.Text(':', font=("Arial", 200, "bold"), text_color = "white") f1 = sg.Frame('', [[a1, t1, a2]], relief="flat") f2 = sg.Frame('', [[a3, t2, a4]], relief="flat") layout1 = [ [f1], [f2] ] z = [0, 0, 0, 0, 0] select = 0 filename = os.path.join(folder, fnames[0]) image_elem = sg.Image(data=get_img_data(filename, first=True)) layout2 = [[image_elem]] # Werbung window2 = sg.Window('Werbung', layout2, size=(1920, 1080), no_titlebar = True, return_keyboard_events=True, finalize=True, element_justification = "center") window2.set_cursor("none") window2.hide() #Spielstand window1 = sg.Window('Spielstand', layout1, size=(1920, 1080), no_titlebar = True, return_keyboard_events=True, finalize=True, location = (0 ,0), element_justification = "center") window1.set_cursor("none") window1.bring_to_front() window1.force_focus() display_score = True i = 0 while True: if display_score: # Spielstand event, values = window1.read(timeout=100) if event is not sg.TIMEOUT_KEY and event is not None: if len(event) == 1: print('%s - %s' % (event, ord(event))) event2 = event else: print(event) event2 = filter(event) zaehlen(event2) if event[0:6] == 'Escape': break if event2 == '*': # swap modes select = 0 zaehlen('u') # nur update von select display_score = not display_score window1.hide() window2.UnHide() window2.bring_to_front() window2.force_focus() print("win2") elif event == sg.WIN_CLOSED: break else: # Werbung event, values = window2.read(timeout=10000) if event is not sg.TIMEOUT_KEY and event is not None: event2 = filter(event) else: event2 = ":0" if event == sg.WIN_CLOSED or event[0:6] == 'Escape': break elif event is sg.TIMEOUT_KEY: print("next") i += 1 if i >= num_files: i -= num_files filename = os.path.join(folder, fnames[i]) elif event2 == '*': display_score = not display_score # swap modes window2.hide() window1.UnHide() window1.bring_to_front() window1.force_focus() print("win1") # update window with new image image_elem.update(data=get_img_data(filename)) print("exit") window1.close() window2.close()