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

时间:2016-08-21 04:06:15

标签: python treeview ttk

我正在使用ttk treeviews构建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")我没有看到指示箭头改变颜色,但正在运行Style.lookup("Treeitem.indicator", "foreground")按预期显示"#123456"。我的计划是将indicatorsize设置为0以使箭头完全消失,但我甚至无法成功编辑颜色。我在这里做错了什么,是否有更好的隐藏指标的方法?如果重要,我正在运行Python 3.5.0。

1 个答案:

答案 0 :(得分:3)

不确定你是否曾经想过它。

创建新样式并对其进行配置时,必须将模板名称更改为"."。这会更改树视图的根样式。您还需要指定一个主题,即使它是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")

给我们

enter image description here enter image description here

存在样式部分的代码

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")

enter image description here enter image description here

希望这有帮助。

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

相关问题