fix:修复一些BUG
This commit is contained in:
92
main.pyw
92
main.pyw
@@ -1,23 +1,81 @@
|
||||
import sys
|
||||
import tkinter as tk
|
||||
from utils.log_handler import setup_logging
|
||||
from ui.app_window import ReportApp
|
||||
|
||||
from loguru import logger
|
||||
|
||||
def main():
|
||||
# 1. 初始化日志
|
||||
setup_logging()
|
||||
logger.info("正在启动应用程序...")
|
||||
from ui.app_window import ReportApp
|
||||
from utils.log_handler import setup_logging
|
||||
|
||||
# 全局变量,用于判断日志是否已初始化
|
||||
LOGGING_INITIALIZED = False
|
||||
|
||||
|
||||
# --- 全局错误处理 ---
|
||||
def handle_exception(exc_type, exc_value, exc_traceback):
|
||||
"""
|
||||
捕获未被 try/except 块处理的全局异常(如线程崩溃)。
|
||||
"""
|
||||
if exc_type is KeyboardInterrupt:
|
||||
sys.__excepthook__(exc_type, exc_value, exc_traceback)
|
||||
return
|
||||
|
||||
# 尝试使用 loguru 记录
|
||||
if LOGGING_INITIALIZED:
|
||||
logger.error("捕获到未处理的全局异常:", exc_info=(exc_type, exc_value, exc_traceback))
|
||||
else:
|
||||
# 如果日志系统未初始化,直接打印到标准错误流,确保用户看到
|
||||
print("FATAL ERROR (Log Not Initialized):", file=sys.stderr)
|
||||
import traceback
|
||||
traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr)
|
||||
|
||||
|
||||
sys.excepthook = handle_exception
|
||||
|
||||
|
||||
# --------------------
|
||||
|
||||
|
||||
def create_main_window():
|
||||
global LOGGING_INITIALIZED
|
||||
|
||||
# 顶级 try 块,捕获日志初始化阶段的错误
|
||||
try:
|
||||
# 1. 初始化日志
|
||||
setup_logging()
|
||||
LOGGING_INITIALIZED = True
|
||||
logger.info("正在启动应用程序...")
|
||||
|
||||
# 2. 启动 UI
|
||||
root = tk.Tk()
|
||||
|
||||
# 这一行可以设置图标 (如果有 icon.ico 文件)
|
||||
# root.iconbitmap(os.path.join(os.path.dirname(__file__), "public", "icon.ico"))
|
||||
|
||||
# 确保 ReportApp 实例化时不会出现路径错误
|
||||
app = ReportApp(root)
|
||||
|
||||
# 3. 进入主循环
|
||||
root.mainloop()
|
||||
|
||||
except Exception as e:
|
||||
# 如果日志系统已启动,使用 logger 记录
|
||||
if LOGGING_INITIALIZED:
|
||||
logger.error(f"应用程序启动/主循环出错: {e}", exc_info=True)
|
||||
else:
|
||||
# 如果日志系统未初始化,直接打印到控制台
|
||||
print(f"FATAL STARTUP ERROR: {e}", file=sys.stderr)
|
||||
import traceback
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
|
||||
# 确保窗口被销毁
|
||||
if 'root' in locals() and root:
|
||||
root.destroy()
|
||||
|
||||
# 非窗口模式下,在启动错误时等待用户查看
|
||||
if not getattr(sys, 'frozen', False) or not any(arg in sys.argv for arg in ('--windowed', '-w')):
|
||||
input("按任意键退出...")
|
||||
sys.exit(1)
|
||||
|
||||
# 2. 启动 UI
|
||||
root = tk.Tk()
|
||||
|
||||
# 这一行可以设置图标 (如果有 icon.ico 文件)
|
||||
# root.iconbitmap("icon.ico")
|
||||
|
||||
app = ReportApp(root)
|
||||
|
||||
# 3. 进入主循环
|
||||
root.mainloop()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
create_main_window()
|
||||
Reference in New Issue
Block a user