#!/usr/bin/env python # Anzeige von Spielständen und Werbung import PySimpleGUI as sg import os from PIL import Image, ImageTk, ImageOps 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 = ImageOps.contain(img, maxsize, method = 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) # ------------------------------------------------------------------------------ # filter key codes in linux, used for key pad # ------------------------------------------------------------------------------ def filter(my_event): r = '' if my_event is not None: if len(my_event) == 1: r = my_event else: #print(my_event) end = my_event[my_event.index(':')+1:] if str.isnumeric(end): code = int(end) # nervige Kodierung der Tasten im RaspPi NumPad! # Basisnummer 79 = 7 etc. numPad = ['7', '8', '9', '-', '4', '5', '6', '+', '1', ' ', '3', '0', ','] if code == 63: r='*' if code>= 79 and code <=91: r = numPad[code-79] return r # ------------------------------------------------------------------------------ # Spielstand zählen # ------------------------------------------------------------------------------ def zaehlen(taste): global z, z_col, select color_cursor = "grey50" #print(taste) flag = False if taste == '1': if select != 3: z_col[select] = "black" select = 3 z_col[3] = color_cursor else: select = 0 z_col[3] = "black" flag = True elif taste == '3': if select != 4: z_col[select] = "black" select = 4 z_col[4] = color_cursor else: select = 0 z_col[4] = "black" flag = True elif taste == '7': if select != 1: z_col[select] = "black" select = 1 z_col[1] = color_cursor else: select = 0 z_col[1] = "black" flag = True elif taste == '9': if select != 2: z_col[select] = "black" select = 2 z_col[2] = color_cursor else: select = 0 z_col[2] = "black" flag = True elif taste == '+': if z[select] < 99: z[select] += 1 #select = 0 flag = True elif taste == '-': if z[select] > 0: z[select] -= 1 #select = 0 flag = True elif taste == '0': if select == 1 or select == 2: z[1:3] = [0, 0] flag = True select = 0 z_col = ['Black' for x in z_col] elif select == 3 or select == 4: z[3:5] = [0, 0] flag = True select = 0 z_col = ['Black' for x in z_col] elif taste == 'u': flag = True z_col = ['Black' for x in z_col] if flag: for i in range(1, 5): window1["-a" + str(i) + "-"].update(z[i], z_col[i]) # ------------------------------------------------------------------------------ # 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 ' + folder + ' !') raise SystemExit() del flist0 # no longer needed sg.theme('Black') a1 = sg.Text('0', font=("Impact", 150), text_color = "blue", justification = "right", size = 2, key = "-a1-") a2 = sg.Text('0', font=("Impact", 150), text_color = "SpringGreen4", justification = "left", size = 2, key = "-a2-") a3 = sg.Text('0', font=("Yu Gothic UI Semibold", 350), text_color = "Red", justification = "right", size = 2, key = "-a3-") a4 = sg.Text('0', font=("Yu Gothic UI Semibold", 350), text_color = "yellow2", justification = "left", size = 2, key = "-a4-") a5 = sg.Text('HEIM ', font=("Impact", 100), text_color = "yellow2", justification = "center", key = "-a5-") a6 = sg.Text(' GAST', font=("Impact", 100), text_color = "yellow2", justification = "center", key = "-a6-") t1 = sg.Text(':', font=("Arial", 150, "bold"), text_color = "white") t2 = sg.Text(':', font=("Arial", 200, "bold"), text_color = "white") layout1 = [[a1, t1, a2], [a5, a6], [a3, t2, a4]] z = [0, 0, 0, 0, 0] z_col = ["black" for i in range(5)] 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") #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: 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 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 = "" 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 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()