🤖 自动格式化代码 [skip ci]
This commit is contained in:
@@ -7,6 +7,7 @@ from typing import Any, Dict, List
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
class ConfigEditor:
|
||||
def __init__(self, root):
|
||||
self.root = root
|
||||
@@ -179,7 +180,7 @@ class ConfigEditor:
|
||||
full_config_path_key = ".".join(path + [key]) # 例如 "chinese_typo.enable"
|
||||
|
||||
item_name_to_display = key # 默认显示原始键名
|
||||
item_desc_to_display = "" # 默认无描述
|
||||
item_desc_to_display = "" # 默认无描述
|
||||
|
||||
# 1. 尝试使用完整路径的特定翻译
|
||||
specific_translation = self.translations.get("items", {}).get(full_config_path_key)
|
||||
@@ -199,22 +200,27 @@ class ConfigEditor:
|
||||
label.grid(row=0, column=0, sticky=tk.W, padx=5, pady=(0, 0))
|
||||
|
||||
# 星星图标快捷设置(与配置名同一行)
|
||||
content_col_offset_for_star = 1 # 星标按钮占一列
|
||||
content_col_offset_for_star = 1 # 星标按钮占一列
|
||||
quick_settings = self.editor_config.get("editor", {}).get("quick_settings", {}).get("items", [])
|
||||
already_in_quick = any(item.get("path") == full_config_path_key for item in quick_settings)
|
||||
icon = "★" if already_in_quick else "☆"
|
||||
icon_fg = "#FFD600" # 始终金色
|
||||
|
||||
def on_star_click():
|
||||
self.toggle_quick_setting(full_config_path_key, widget_type, item_name_to_display, item_desc_to_display, already_in_quick)
|
||||
self.toggle_quick_setting(
|
||||
full_config_path_key, widget_type, item_name_to_display, item_desc_to_display, already_in_quick
|
||||
)
|
||||
# 立即刷新本分组
|
||||
for widget in parent.winfo_children():
|
||||
widget.destroy()
|
||||
self.widgets.clear()
|
||||
# 重新渲染本分组
|
||||
if hasattr(self, 'current_section') and self.current_section and self.current_section != "quick_settings":
|
||||
self.create_section_widgets(parent, self.current_section, self.config[self.current_section], [self.current_section])
|
||||
elif hasattr(self, 'current_section') and self.current_section == "quick_settings":
|
||||
self.create_quick_settings_widgets() # 如果当前是快捷设置,也刷新它
|
||||
if hasattr(self, "current_section") and self.current_section and self.current_section != "quick_settings":
|
||||
self.create_section_widgets(
|
||||
parent, self.current_section, self.config[self.current_section], [self.current_section]
|
||||
)
|
||||
elif hasattr(self, "current_section") and self.current_section == "quick_settings":
|
||||
self.create_quick_settings_widgets() # 如果当前是快捷设置,也刷新它
|
||||
|
||||
pin_btn = ttk.Button(frame, text=icon, width=2, command=on_star_click)
|
||||
pin_btn.grid(row=0, column=content_col_offset_for_star, sticky=tk.W, padx=5)
|
||||
@@ -229,22 +235,24 @@ class ConfigEditor:
|
||||
desc_row = 1
|
||||
if item_desc_to_display:
|
||||
desc_label = ttk.Label(frame, text=item_desc_to_display, foreground="gray", font=("", 16))
|
||||
desc_label.grid(row=desc_row, column=0, columnspan=content_col_offset_for_star + 1, sticky=tk.W, padx=5, pady=(0, 4))
|
||||
widget_row = desc_row + 1 # 内容控件在描述下方
|
||||
desc_label.grid(
|
||||
row=desc_row, column=0, columnspan=content_col_offset_for_star + 1, sticky=tk.W, padx=5, pady=(0, 4)
|
||||
)
|
||||
widget_row = desc_row + 1 # 内容控件在描述下方
|
||||
else:
|
||||
widget_row = desc_row # 内容控件直接在第二行
|
||||
widget_row = desc_row # 内容控件直接在第二行
|
||||
|
||||
# 配置内容控件(第三行或第二行)
|
||||
if path[0] == "inner":
|
||||
value_label = ttk.Label(frame, text=str(value), font=("", 20))
|
||||
value_label.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star +1, sticky=tk.W, padx=5)
|
||||
value_label.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star + 1, sticky=tk.W, padx=5)
|
||||
return
|
||||
|
||||
if isinstance(value, bool):
|
||||
# 布尔值使用复选框
|
||||
var = tk.BooleanVar(value=value)
|
||||
checkbox = ttk.Checkbutton(frame, variable=var, command=lambda: self.on_value_changed())
|
||||
checkbox.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star +1, sticky=tk.W, padx=5)
|
||||
checkbox.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star + 1, sticky=tk.W, padx=5)
|
||||
self.widgets[tuple(path + [key])] = var
|
||||
widget_type = "bool"
|
||||
|
||||
@@ -252,7 +260,7 @@ class ConfigEditor:
|
||||
# 数字使用数字输入框
|
||||
var = tk.StringVar(value=str(value))
|
||||
entry = ttk.Entry(frame, textvariable=var, font=("", 20))
|
||||
entry.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star +1, sticky=tk.W+tk.E, padx=5)
|
||||
entry.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star + 1, sticky=tk.W + tk.E, padx=5)
|
||||
var.trace_add("write", lambda *args: self.on_value_changed())
|
||||
self.widgets[tuple(path + [key])] = var
|
||||
widget_type = "number"
|
||||
@@ -260,13 +268,17 @@ class ConfigEditor:
|
||||
elif isinstance(value, list):
|
||||
# 列表使用每行一个输入框的形式
|
||||
frame_list = ttk.Frame(frame)
|
||||
frame_list.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star +1, sticky=tk.W+tk.E, padx=5)
|
||||
frame_list.grid(
|
||||
row=widget_row, column=0, columnspan=content_col_offset_for_star + 1, sticky=tk.W + tk.E, padx=5
|
||||
)
|
||||
|
||||
# 创建添加和删除按钮
|
||||
button_frame = ttk.Frame(frame_list)
|
||||
button_frame.pack(side=tk.RIGHT, padx=5)
|
||||
|
||||
add_button = ttk.Button(button_frame, text="+", width=3, command=lambda p=path + [key]: self.add_list_item(frame_list, p))
|
||||
add_button = ttk.Button(
|
||||
button_frame, text="+", width=3, command=lambda p=path + [key]: self.add_list_item(frame_list, p)
|
||||
)
|
||||
add_button.pack(side=tk.TOP, pady=2)
|
||||
|
||||
# 创建列表项框架
|
||||
@@ -288,7 +300,7 @@ class ConfigEditor:
|
||||
# 其他类型(字符串等)使用普通文本框
|
||||
var = tk.StringVar(value=str(value))
|
||||
entry = ttk.Entry(frame, textvariable=var, font=("", 20))
|
||||
entry.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star +1, sticky=tk.W+tk.E, padx=5)
|
||||
entry.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star + 1, sticky=tk.W + tk.E, padx=5)
|
||||
var.trace_add("write", lambda *args: self.on_value_changed())
|
||||
self.widgets[tuple(path + [key])] = var
|
||||
widget_type = "text"
|
||||
@@ -387,7 +399,7 @@ class ConfigEditor:
|
||||
|
||||
# 创建描述标签
|
||||
if setting.get("description"):
|
||||
desc_label = ttk.Label(frame, text=setting['description'], foreground="gray", font=("", 16))
|
||||
desc_label = ttk.Label(frame, text=setting["description"], foreground="gray", font=("", 16))
|
||||
desc_label.pack(fill=tk.X, padx=5, pady=(0, 2))
|
||||
|
||||
# 根据类型创建不同的控件
|
||||
@@ -396,30 +408,31 @@ class ConfigEditor:
|
||||
if setting_type == "bool":
|
||||
value = bool(value) if value is not None else False
|
||||
var = tk.BooleanVar(value=value)
|
||||
checkbox = ttk.Checkbutton(frame, text="",
|
||||
variable=var,
|
||||
command=lambda p=path, v=var: self.on_quick_setting_changed(p, v))
|
||||
checkbox.pack(anchor=tk.W, padx=5, pady=(0,5))
|
||||
checkbox = ttk.Checkbutton(
|
||||
frame, text="", variable=var, command=lambda p=path, v=var: self.on_quick_setting_changed(p, v)
|
||||
)
|
||||
checkbox.pack(anchor=tk.W, padx=5, pady=(0, 5))
|
||||
|
||||
elif setting_type == "text":
|
||||
value = str(value) if value is not None else ""
|
||||
var = tk.StringVar(value=value)
|
||||
entry = ttk.Entry(frame, textvariable=var, width=40, font=("", 18))
|
||||
entry.pack(fill=tk.X, padx=5, pady=(0,5))
|
||||
entry.pack(fill=tk.X, padx=5, pady=(0, 5))
|
||||
var.trace_add("write", lambda *args, p=path, v=var: self.on_quick_setting_changed(p, v))
|
||||
|
||||
elif setting_type == "number":
|
||||
value = str(value) if value is not None else "0"
|
||||
var = tk.StringVar(value=value)
|
||||
entry = ttk.Entry(frame, textvariable=var, width=10, font=("", 18))
|
||||
entry.pack(fill=tk.X, padx=5, pady=(0,5))
|
||||
entry.pack(fill=tk.X, padx=5, pady=(0, 5))
|
||||
var.trace_add("write", lambda *args, p=path, v=var: self.on_quick_setting_changed(p, v))
|
||||
|
||||
elif setting_type == "list":
|
||||
# 对于列表类型,创建一个按钮来打开编辑窗口
|
||||
button = ttk.Button(frame, text="编辑列表",
|
||||
command=lambda p=path, s=setting: self.open_list_editor(p, s))
|
||||
button.pack(anchor=tk.W, padx=5, pady=(0,5))
|
||||
button = ttk.Button(
|
||||
frame, text="编辑列表", command=lambda p=path, s=setting: self.open_list_editor(p, s)
|
||||
)
|
||||
button.pack(anchor=tk.W, padx=5, pady=(0, 5))
|
||||
|
||||
def create_list_item(self, parent, value, index, entry_vars, path):
|
||||
"""创建单个列表项的输入框"""
|
||||
@@ -433,8 +446,12 @@ class ConfigEditor:
|
||||
var.trace_add("write", lambda *args: self.on_value_changed())
|
||||
|
||||
# 创建删除按钮
|
||||
del_button = ttk.Button(item_frame, text="-", width=3,
|
||||
command=lambda: self.remove_list_item(parent, item_frame, entry_vars, index, path))
|
||||
del_button = ttk.Button(
|
||||
item_frame,
|
||||
text="-",
|
||||
width=3,
|
||||
command=lambda: self.remove_list_item(parent, item_frame, entry_vars, index, path),
|
||||
)
|
||||
del_button.pack(side=tk.RIGHT, padx=5)
|
||||
|
||||
# 存储变量引用
|
||||
@@ -498,7 +515,9 @@ class ConfigEditor:
|
||||
self.last_save_time = time.time()
|
||||
self.pending_save = False
|
||||
self.editor_title.config(text=f"{self.editor_title.cget('text')} (已保存)")
|
||||
self.root.after(2000, lambda: self.editor_title.config(text=self.editor_title.cget('text').replace(" (已保存)", "")))
|
||||
self.root.after(
|
||||
2000, lambda: self.editor_title.config(text=self.editor_title.cget("text").replace(" (已保存)", ""))
|
||||
)
|
||||
except Exception as e:
|
||||
messagebox.showerror("错误", f"保存配置失败: {str(e)}")
|
||||
|
||||
@@ -549,13 +568,13 @@ class ConfigEditor:
|
||||
button_frame.pack(fill=tk.X, pady=10)
|
||||
|
||||
# 添加按钮
|
||||
add_button = ttk.Button(button_frame, text="添加",
|
||||
command=lambda: self.add_list_item(items_frame, path))
|
||||
add_button = ttk.Button(button_frame, text="添加", command=lambda: self.add_list_item(items_frame, path))
|
||||
add_button.pack(side=tk.LEFT, padx=5)
|
||||
|
||||
# 保存按钮
|
||||
save_button = ttk.Button(button_frame, text="保存",
|
||||
command=lambda: self.save_list_editor(dialog, path, entry_vars))
|
||||
save_button = ttk.Button(
|
||||
button_frame, text="保存", command=lambda: self.save_list_editor(dialog, path, entry_vars)
|
||||
)
|
||||
save_button.pack(side=tk.RIGHT, padx=5)
|
||||
|
||||
def save_list_editor(self, dialog, path, entry_vars):
|
||||
@@ -602,21 +621,21 @@ class ConfigEditor:
|
||||
self.on_value_changed()
|
||||
|
||||
def toggle_quick_setting(self, full_path, widget_type, name, desc, already_in_quick):
|
||||
quick_settings = self.editor_config.setdefault("editor", {}).setdefault("quick_settings", {}).setdefault("items", [])
|
||||
quick_settings = (
|
||||
self.editor_config.setdefault("editor", {}).setdefault("quick_settings", {}).setdefault("items", [])
|
||||
)
|
||||
if already_in_quick:
|
||||
# 移除
|
||||
self.editor_config["editor"]["quick_settings"]["items"] = [item for item in quick_settings if item.get("path") != full_path]
|
||||
self.editor_config["editor"]["quick_settings"]["items"] = [
|
||||
item for item in quick_settings if item.get("path") != full_path
|
||||
]
|
||||
else:
|
||||
# 添加
|
||||
quick_settings.append({
|
||||
"name": name,
|
||||
"description": desc,
|
||||
"path": full_path,
|
||||
"type": widget_type
|
||||
})
|
||||
quick_settings.append({"name": name, "description": desc, "path": full_path, "type": widget_type})
|
||||
# 保存到configexe.toml
|
||||
import tomli_w
|
||||
import os
|
||||
|
||||
config_path = os.path.join(os.path.dirname(__file__), "configexe.toml")
|
||||
with open(config_path, "wb") as f:
|
||||
tomli_w.dump(self.editor_config, f)
|
||||
@@ -630,10 +649,12 @@ class ConfigEditor:
|
||||
self.widgets.clear()
|
||||
self.create_quick_settings_widgets()
|
||||
|
||||
|
||||
def main():
|
||||
root = tk.Tk()
|
||||
_app = ConfigEditor(root)
|
||||
root.mainloop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user