You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ttk.TreeView渲染异常:左侧出现大空白列问题求助

解决ttk TreeView左侧空白列问题

这个左侧空白列是TreeView组件默认自带的树形列(标识为#0),它原本用于显示层级结构(比如父子节点缩进),由于你未对该列做任何设置,所以默认显示为空白列。

有两种简单的解决方式:

方法1:隐藏默认树形列

在初始化TreeView后,添加代码隐藏#0列:

cols = ("EIR", "Env", "Name", "Function", "IP", "Model")
col_widths = (75, 50, 120, 150, 100, 150)
self.tv = tv = ttk.Treeview(self, columns=cols)
# 隐藏默认的#0列
tv.column("#0", width=0, stretch=tk.NO)
for col, col_w in zip(cols, col_widths):
  tv.column(column=col, width=col_w, anchor=tk.W)
  tv.heading(column=col, text=col, anchor=tk.W)

方法2:初始化时指定只显示自定义列

创建TreeView时添加show="headings"参数,组件将只渲染你定义的列,默认树形列直接不显示:

cols = ("EIR", "Env", "Name", "Function", "IP", "Model")
col_widths = (75, 50, 120, 150, 100, 150)
# 添加show="headings"参数
self.tv = tv = ttk.Treeview(self, columns=cols, show="headings")
for col, col_w in zip(cols, col_widths):
  tv.column(column=col, width=col_w, anchor=tk.W)
  tv.heading(column=col, text=col, anchor=tk.W)

两种方法都能解决左侧空白列问题,方法2更直接,适合不需要树形层级结构的场景。

内容的提问来源于stack exchange,提问作者WV_Mapper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:42:35