From eb23d2d8e6793b417a0d384d3217cbc73cc194f8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 17 Jan 2026 22:00:14 +0800 Subject: [PATCH] =?UTF-8?q?GUI:=20=E8=87=AA=E5=8A=A8=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E6=98=A0=E5=B0=84=E8=A1=A8=E5=88=97=E5=AE=BD=E4=BB=A5=E9=80=82?= =?UTF-8?q?=E9=85=8D=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/gui_app.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/gui/gui_app.py b/gui/gui_app.py index 170f09a..284803f 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -2830,6 +2830,77 @@ def main(): mapping_tree.tag_configure("odd", background="#fafafa") mapping_tree.tag_configure("even", background="#ffffff") + # 自动调整列宽以适应内容(基于文本像素宽度测量) + try: + import tkinter.font as tkfont + + def _adjust_mapping_tree_columns(tree: ttk.Treeview, padding: int = 12, max_width: int = 1200) -> None: + try: + font = tkfont.Font() + except Exception: + try: + # 回退:创建默认字体 + font = tkfont.Font() + except Exception: + font = None + + # 最小列宽(与之前的默认宽度保持一致,避免太窄) + min_widths = { + "pos": 50, + "title": 120, + "title_raw": 120, + "has_fudai": 60, + "fudai_content": 180, + "task_list": 160, + "countdown": 90, + "fudai_value": 90, + "participants": 90, + "copies": 70, + "button_text": 140, + "is_reserved": 70, + } + + cols = tree["columns"] + for col in cols: + try: + # 获取表头文本 + header = tree.heading(col).get("text", str(col)) + except Exception: + try: + header = tree.heading(col, "text") + except Exception: + header = str(col) + + try: + max_w = font.measure(str(header)) if font else len(str(header)) * 8 + except Exception: + max_w = len(str(header)) * 8 + + # 遍历所有行,找到最大内容宽度 + for iid in tree.get_children(): + try: + cell = str(tree.set(iid, col) or "") + except Exception: + cell = "" + try: + w = font.measure(cell) if font else len(cell) * 8 + except Exception: + w = len(cell) * 8 + if w > max_w: + max_w = w + + target = int(max(min_widths.get(col, 40), max_w + padding)) + if target > max_width: + target = max_width + try: + tree.column(col, width=target) + except Exception: + pass + except Exception: + # tkinter.font 不可用时跳过自动调整 + def _adjust_mapping_tree_columns(tree: ttk.Treeview, padding: int = 12, max_width: int = 1200) -> None: + return + map_scroll_y = ttk.Scrollbar( mapping_tree_box, orient="vertical", command=mapping_tree.yview ) @@ -3119,6 +3190,13 @@ def main(): # 忽略单个路径读取错误,尝试下一个候选 pass + # 如果已找到并填充了映射表,尝试自动调整列宽以适配内容 + if found: + try: + _adjust_mapping_tree_columns(mapping_tree) + except Exception: + pass + if not found: mapping_source_var.set("未找到 room_pos_map.json") mapping_tree.insert(