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

标签 python treeview ttk

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

如果我运行 Style().element_names(),我会看到有一个 Treeview 元素和一个 Treeitem.indicator 元素。如果我运行 Style().configure("Treeview", padding=50),我看到在创建 TreeView 时应用了填充样式,所以我确信我正确地应用到 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"。我的计划是将指示器大小设置为 0 以使箭头完全消失,但我什至无法成功编辑颜色。我在这里做错了什么,有没有更好的方法来隐藏指标?以防万一,我正在运行 Python 3.5.0。

最佳答案

不确定你是否想通了。

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

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

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

让我知道这是否适合您。

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

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

    #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 主题。

关于python - 如何在 Python GUI 中隐藏 ttk Treeitem 指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39060756/

相关文章:

python - pandas.Series.div() vs/=

python - 读取制表符分隔的文件,并将每一列分配给一个独立的变量

python - 查找一组数据的中位数并应用于该组的成员

c# - 通过属性设置 Treeview HierarchicalDataTemplate

button - Python3 : How to dynamically resize button text in tkinter/ttk?

python - 打印列表列表的索引作为主列表(python)

python - 如何在 GTK+3 TreeView 上禁用 "selection by clicking "

C# 手动排序列表中的父/子树元素

python - 为什么 tkfont.Font().measure 返回大值?

python - 删除 tkinter TreeView 中的标题行