Fallback to Flask UI when tkinter/_tkinter missing; avoid NameError and keep app launchable via Finder

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
Pat TeruelandCopilot committed 2026-03-01 10:49:10 +08:00
1 parent 6505781139
commit 4cc008b211
1 file changed
+36 -1
+36 -1
View File
@@ -5,9 +5,16 @@ Native GUI using tkinter to generate One Piece progress reports without a web se
import os
import threading
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
from datetime import datetime
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
@@ -161,7 +168,35 @@ def main():
if __name__ == '__main__':
try:
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()