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:
1 parent
6505781139
commit
4cc008b211
1 file changed
+37
-2
+37
-2
@@ -5,10 +5,17 @@ Native GUI using tkinter to generate One Piece progress reports without a web se
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
import tkinter as tk
|
import webbrowser
|
||||||
from tkinter import ttk, messagebox, scrolledtext
|
|
||||||
from datetime import datetime
|
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
|
# Reuse helper logic from app.py
|
||||||
from app import current_week, format_date_range, get_latest_sunday, get_previous_sunday
|
from app import current_week, format_date_range, get_latest_sunday, get_previous_sunday
|
||||||
from clients import jellyfin
|
from clients import jellyfin
|
||||||
@@ -161,7 +168,35 @@ def main():
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
|
if TK_AVAILABLE:
|
||||||
main()
|
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:
|
except Exception as e:
|
||||||
import traceback, sys, os
|
import traceback, sys, os
|
||||||
tb = traceback.format_exc()
|
tb = traceback.format_exc()
|
||||||
|
|||||||
Reference in new issue
Block a user