standalone_mode.py 987 B

1234567891011121314151617181920212223242526272829
  1. import multiprocessing
  2. import os
  3. import signal
  4. import tempfile
  5. import time
  6. from threading import Thread
  7. import webview
  8. shutdown = multiprocessing.Event()
  9. def open_window(url: str, title: str, width: int, height: int, fullscreen: bool, shutdown: multiprocessing.Event) -> None:
  10. window = webview.create_window(title, url=url, width=width, height=height, fullscreen=fullscreen)
  11. window.events.closing += shutdown.set # signal that the program should be closed to the main process
  12. webview.start(storage_path=tempfile.mkdtemp())
  13. def check_shutdown() -> None:
  14. while True:
  15. if shutdown.is_set():
  16. os.kill(os.getpgid(os.getpid()), signal.SIGTERM)
  17. time.sleep(0.1)
  18. def activate(url: str, title: str, width: int, height: int, fullscreen: bool) -> None:
  19. args = url, title, width, height, fullscreen, shutdown
  20. multiprocessing.Process(target=open_window, args=args, daemon=False).start()
  21. Thread(target=check_shutdown, daemon=True).start()