From 4cc008b211953de0fbb61b37b10fa0b818d32625 Mon Sep 17 00:00:00 2001 From: Pat Teruel Date: Sun, 1 Mar 2026 10:49:10 +0800 Subject: [PATCH] Fallback to Flask UI when tkinter/_tkinter missing; avoid NameError and keep app launchable via Finder Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- mac_native.py | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/mac_native.py b/mac_native.py index 8ff9abc..2c0398f 100644 --- a/mac_native.py +++ b/mac_native.py @@ -5,10 +5,17 @@ Native GUI using tkinter to generate One Piece progress reports without a web se import os import threading -import tkinter as tk -from tkinter import ttk, messagebox, scrolledtext +import webbrowser from datetime import datetime +# Try to import tkinter; if unavailable, fall back to launching the Flask web UI +try: + import tkinter as tk + from tkinter import ttk, messagebox, scrolledtext + TK_AVAILABLE = True +except Exception: + TK_AVAILABLE = False + # Reuse helper logic from app.py from app import current_week, format_date_range, get_latest_sunday, get_previous_sunday from clients import jellyfin @@ -161,7 +168,35 @@ def main(): if __name__ == '__main__': try: - main() + if TK_AVAILABLE: + main() + else: + # Fallback: launch the Flask web UI in a background thread and open the browser + try: + from app import app + except Exception as _e: + raise RuntimeError(f"Tkinter not available and Flask app failed to import: {_e}") + + def _run_server(): + port = int(os.environ.get("PORT", 5000)) + app.run(host="127.0.0.1", port=port, debug=False, use_reloader=False) + + t = threading.Thread(target=_run_server, daemon=True) + t.start() + # Give the server a moment to start + import time + time.sleep(1.0) + port = os.environ.get("PORT", "5000") + try: + webbrowser.open(f"http://127.0.0.1:{port}/") + except Exception: + pass + + try: + while t.is_alive(): + t.join(0.5) + except KeyboardInterrupt: + pass except Exception as e: import traceback, sys, os tb = traceback.format_exc()