如何在 Python GUI 中隐藏 ttk Treeitem 指标

How to hide ttk Treeitem indicators in a Python GUI

我正在使用 ttk 树视图构建一个 Python GUI 应用程序。在 Linux 上,当 Treeitem 有子项时,它会显示一个箭头以表明该行可以展开。我想隐藏这个指示箭头(我正在使用其他方式来暗示该行可以展开)。我该怎么做?

如果我 运行 Style().element_names() 我看到有一个 Treeview 元素和一个 Treeitem.indicator 元素。如果我 运行 Style().configure("Treeview", padding=50),我看到在创建树视图时应用了填充样式,所以我相信我正确应用到 Treeitem.indicator 的任何样式也应该是可见的。

运行Style().element_options("Treeitem.indicator"),我看到了('-foreground', '-indicatorsize', '-indicatormargins')。 运行 Style().lookup("Treeitem.indicator", "foreground") 给我 "#000000",因此值似乎已初始化。如果我尝试 Style().configure("Treeview", foreground="#123456") 我没有看到指示器箭头改变颜色,尽管 运行ning Style.lookup("Treeitem.indicator", "foreground") 显示了我预期的 "#123456"。我的计划是将指示器大小设置为 0 以使箭头完全消失,但我什至无法成功编辑颜色。我在这里做错了什么,有没有更好的方法来隐藏指标?以防万一,我是 运行ning Python 3.5.0.

不确定你是否想通了。

创建新样式并对其进行配置时,必须将模板名称更改为"."。这会更改树视图的根样式。您还需要指定一个主题,即使它是 default。所以它看起来像:

s = ttk.Style()
s.configure(".", indicatorsize = '0')
s.theme_use('default')

那么当您创建树视图时,您根本不需要指定样式。

让我知道这是否适合你。

编辑:由于某些原因这被否决了,我将澄清:

样式部分被注释掉的代码:

    #s = ttk.Style()
    #s.configure(".", indicatorsize = '0')
    #s.theme_use('clam')

    j = ttk.Treeview(self.parent)
    j.place(relx = 0.5, rely = 0.5, anchor = "center")
    j.insert("",1,"jacob",text = "Jacob")
    j.insert("jacob",1,"marcus",text = "Marcus")
    j.insert("jacob",2,"tony",text = "Tony")
    j.insert("jacob",3,"ricardo",text = "Ricardo")

给我们

带有样式部分的代码

s = ttk.Style()
s.configure(".", indicatorsize = '0')
s.theme_use('clam')

j = ttk.Treeview(self.parent)
j.place(relx = 0.5, rely = 0.5, anchor = "center")
j.insert("",1,"jacob",text = "Jacob")
j.insert("jacob",1,"marcus",text = "Marcus")
j.insert("jacob",2,"tony",text = "Tony")
j.insert("jacob",3,"ricardo",text = "Ricardo")

希望这对您有所帮助。

编辑 2: 添加了 s.theme_use('clam') 行,因为您需要指定您正在使用的主题。它也适用于 classicdefault,但由于某些原因不适用于 vista 主题。