This commit is contained in:
SengokuCola
2025-04-07 19:27:33 +08:00
parent 225fbbe6ea
commit 33d7305b4b

View File

@@ -2,10 +2,8 @@ import os
import sys import sys
import toml import toml
import customtkinter as ctk import customtkinter as ctk
from tkinter import messagebox, StringVar, Text, filedialog from tkinter import messagebox, StringVar, filedialog
import json import json
from typing import Dict, Any, List, Union, Optional
import re
import datetime import datetime
import shutil import shutil
@@ -734,7 +732,7 @@ class ConfigUI(ctk.CTk):
"""保存配置文件""" """保存配置文件"""
# 更新配置数据 # 更新配置数据
updated = False updated = False
error_path = None _error_path = None
for path, (var, var_type, *args) in self.config_vars.items(): for path, (var, var_type, *args) in self.config_vars.items():
parts = path.split(".") parts = path.split(".")
@@ -753,26 +751,22 @@ class ConfigUI(ctk.CTk):
target[parts[-1]] = var.get() target[parts[-1]] = var.get()
updated = True updated = True
elif var_type == "number": elif var_type == "number":
try:
# 获取原始类型int或float # 获取原始类型int或float
num_type = args[0] if args else int num_type = args[0] if args else int
new_value = num_type(var.get()) new_value = num_type(var.get())
if target[parts[-1]] != new_value: if target[parts[-1]] != new_value:
target[parts[-1]] = new_value target[parts[-1]] = new_value
updated = True updated = True
except ValueError:
error_path = path
raise ValueError(f"{path} 必须是一个有效的数字!")
elif var_type == "list": elif var_type == "list":
try:
# 解析JSON字符串为列表 # 解析JSON字符串为列表
new_value = json.loads(var.get()) new_value = json.loads(var.get())
if json.dumps(target[parts[-1]], sort_keys=True) != json.dumps(new_value, sort_keys=True): if json.dumps(target[parts[-1]], sort_keys=True) != json.dumps(new_value, sort_keys=True):
target[parts[-1]] = new_value target[parts[-1]] = new_value
updated = True updated = True
except json.JSONDecodeError:
error_path = path
raise ValueError(f"{path} 必须是有效的JSON格式")
else: else:
if target[parts[-1]] != var.get(): if target[parts[-1]] != var.get():
target[parts[-1]] = var.get() target[parts[-1]] = var.get()
@@ -978,7 +972,7 @@ class ConfigUI(ctk.CTk):
backup_frame.pack(padx=10, pady=10, fill="both", expand=True) backup_frame.pack(padx=10, pady=10, fill="both", expand=True)
# 添加备份文件项 # 添加备份文件项
for i, (filename, filepath, mod_time) in enumerate(backup_files): for _i, (filename, filepath, mod_time) in enumerate(backup_files):
# 格式化时间为可读格式 # 格式化时间为可读格式
time_str = datetime.datetime.fromtimestamp(mod_time).strftime("%Y-%m-%d %H:%M:%S") time_str = datetime.datetime.fromtimestamp(mod_time).strftime("%Y-%m-%d %H:%M:%S")
@@ -1144,7 +1138,7 @@ class ConfigUI(ctk.CTk):
# 配置项路径 # 配置项路径
path_parts = full_path.split(".") path_parts = full_path.split(".")
section = path_parts[0] if len(path_parts) > 0 else "" section = path_parts[0] if len(path_parts) > 0 else ""
key = path_parts[-1] if len(path_parts) > 0 else "" _key = path_parts[-1] if len(path_parts) > 0 else ""
# 获取描述 # 获取描述
description = get_description(full_path) description = get_description(full_path)
@@ -1368,15 +1362,14 @@ def main():
except Exception as e: except Exception as e:
print(f"程序发生错误: {e}") print(f"程序发生错误: {e}")
# 显示错误对话框 # 显示错误对话框
try:
import tkinter as tk import tkinter as tk
from tkinter import messagebox from tkinter import messagebox
root = tk.Tk() root = tk.Tk()
root.withdraw() root.withdraw()
messagebox.showerror("程序错误", f"程序运行时发生错误:\n{e}") messagebox.showerror("程序错误", f"程序运行时发生错误:\n{e}")
root.destroy() root.destroy()
except:
pass
if __name__ == "__main__": if __name__ == "__main__":
main() main()