"""Full-screen display for a Raspberry Pi at the table: runs pair_device.py next to this file and shows what it prints, the code large, readable from two metres. With the packages qrcode and Pillow installed (sudo apt install python3-qrcode python3-pil python3-pil.imagetk) it also shows the QR code. Python 3.9 or newer with tkinter (sudo apt install python3-tk). python3 display.py Should pair_device.py end with an error, display.py starts it again after RESTART_SECONDS and shows until then what it printed last. Escape closes the window. """ import os import queue import re import subprocess import sys import threading import time import tkinter as tk from pathlib import Path PAIR_DEVICE = Path(__file__).with_name("pair_device.py") CODE = re.compile(r"Code am Tisch: ([A-Z0-9]{3} [A-Z0-9]{3})") LINK = re.compile(r"https://\S+/verbinden\?code=[A-Z0-9]{6}") # Seconds before pair_device.py starts again after it ended with an error. RESTART_SECONDS = 30 # Light type on a dark ground, more than 4.5:1. BACKGROUND, FOREGROUND, ACCENT = "#15160f", "#f6f6ef", "#d6d63a" root = tk.Tk() root.configure(background=BACKGROUND, cursor="none") root.attributes("-fullscreen", True) root.bind("", lambda _: root.destroy()) height = root.winfo_screenheight() code_label = tk.Label(root, text="", font=("DejaVu Sans Mono", height // 6, "bold"), fg=FOREGROUND, bg=BACKGROUND) qr_label = tk.Label(root, bg=BACKGROUND) status_label = tk.Label(root, text="", font=("DejaVu Sans", height // 28), fg=ACCENT, bg=BACKGROUND, wraplength=root.winfo_screenwidth() - 80, justify="center") code_label.pack(expand=True) qr_label.pack() status_label.pack(expand=True) def show_qr(link): try: import qrcode from PIL import ImageTk except ImportError: return image = qrcode.make(link, border=2).resize((height // 3, height // 3)) qr_label.image = ImageTk.PhotoImage(image) qr_label.configure(image=qr_label.image) def show(line): """One line of pair_device.py: the code, the link for the QR code, or a message in words.""" if match := CODE.search(line): code_label.configure(text=match.group(1)) elif match := LINK.search(line): show_qr(match.group(0)) elif line.strip(): if line.startswith("Verbunden"): code_label.configure(text="") qr_label.configure(image="") status_label.configure(text=line.strip()) def read_output(): """Runs pair_device.py until it ends with 0, paired. After an error it waits and starts it again; without this the screen would keep the last line, and systemd would not notice.""" # Unbuffered, so every line arrives as soon as pair_device.py prints it. environment = {**os.environ, "PYTHONUNBUFFERED": "1", "PYTHONIOENCODING": "utf-8"} while True: process = subprocess.Popen([sys.executable, str(PAIR_DEVICE)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding="utf-8", env=environment) for line in process.stdout: lines.put(line) if process.wait() == 0: return time.sleep(RESTART_SECONDS) def show_waiting_lines(): # tkinter belongs to its own thread; the reader hands the lines over through a queue. while not lines.empty(): show(lines.get()) root.after(200, show_waiting_lines) lines = queue.Queue() threading.Thread(target=read_output, daemon=True).start() show_waiting_lines() root.mainloop()