GUI: 自动调整映射表列宽以适配内容
This commit is contained in:
1 parent
272f3ddae0
commit
eb23d2d8e6
1 file changed
+78
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user