#!/usr/bin/env python3 """ Kiosk PyQt6 WebEngine pour photobooth. Alternative au browser : process unique, pas de "already running", restart propre. Usage : python3 scripts/kiosk-qt.py """ import sys import time import subprocess from PyQt6.QtCore import Qt, QUrl, QTimer from PyQt6.QtWidgets import QApplication from PyQt6.QtWebEngineWidgets import QWebEngineView from PyQt6.QtWebEngineCore import QWebEngineSettings BACKEND_URL = "http://localhost:8080" RETRY_INTERVAL_MS = 3000 def wait_backend(url: str, timeout: int = 30) -> bool: """Attend que le backend soit dispo avant d'afficher.""" import urllib.request for _ in range(timeout): try: urllib.request.urlopen(url, timeout=1) return True except Exception: time.sleep(1) return False class KioskWindow(QWebEngineView): def __init__(self): super().__init__() # Plein ecran sans decoration self.setWindowFlags(Qt.WindowType.FramelessWindowHint) self.showFullScreen() # Parametres WebEngine settings = self.settings() settings.setAttribute(QWebEngineSettings.WebAttribute.JavascriptEnabled, True) settings.setAttribute(QWebEngineSettings.WebAttribute.LocalStorageEnabled, True) settings.setAttribute(QWebEngineSettings.WebAttribute.TouchIconsEnabled, True) settings.setAttribute(QWebEngineSettings.WebAttribute.ScrollAnimatorEnabled, True) # Cacher le curseur self.setCursor(Qt.CursorShape.BlankCursor) self.setUrl(QUrl(BACKEND_URL)) # Watchdog : recharge si la page ne repond plus (WebSocket coupe) self._watchdog = QTimer(self) self._watchdog.setInterval(10000) self._watchdog.timeout.connect(self._check_backend) self._watchdog.start() def _check_backend(self): import urllib.request try: urllib.request.urlopen(BACKEND_URL, timeout=2) except Exception: self.reload() def keyPressEvent(self, event): # F5 = reload, F11 = fullscreen toggle, Echap ignoré if event.key() == Qt.Key.Key_F5: self.reload() elif event.key() == Qt.Key.Key_F11: if self.isFullScreen(): self.showNormal() else: self.showFullScreen() def main(): app = QApplication(sys.argv) app.setApplicationName("Photobooth Kiosk") # Attendre que le backend soit pret if not wait_backend(BACKEND_URL): print("Backend non disponible apres 30s", file=sys.stderr) sys.exit(1) window = KioskWindow() sys.exit(app.exec()) if __name__ == "__main__": main()