🤖 自动格式化代码 [skip ci]

This commit is contained in:
github-actions[bot]
2025-05-29 12:20:47 +00:00
parent 162f796572
commit 3996d8623c

View File

@@ -7,6 +7,7 @@ from typing import Any, Dict, List
import threading
import time
class ConfigEditor:
def __init__(self, root):
self.root = root
@@ -204,16 +205,21 @@ class ConfigEditor:
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":
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)
@@ -229,7 +235,9 @@ 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))
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 # 内容控件直接在第二行
@@ -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)
# 创建列表项框架
@@ -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,9 +408,9 @@ 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 = 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":
@@ -417,8 +429,9 @@ class ConfigEditor:
elif setting_type == "list":
# 对于列表类型,创建一个按钮来打开编辑窗口
button = ttk.Button(frame, text="编辑列表",
command=lambda p=path, s=setting: self.open_list_editor(p, s))
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()