我只是不喜欢这些+-按钮(显示或隐藏子数据)的样子,在我看来已经过时了.我想将它们更改为一些其他元素(如简单的三角形或图像/图标).因此,改变这一点:

enter image description here

类似于这样的事情:

enter image description here

我以为斯泰尔斯能帮上忙,但似乎没有办法改变这些按钮的外观.也许一个原因是,据我所知,这些按钮取决于使用的系统(对于微软来说,它们是+-,在Lunix上可能是三角形),所以也许这就是我无法配置它们的原因.

推荐答案

我找到了一种不使用主题的方法.它不是那么复杂,但需要知识来使用它.

Light solution:

首先,我创建了3个大小均为16x16像素的图像.这个尺寸完全适合线条之间的分隔,但您可以将其更改为更适合您的尺寸.

Secondly, I am changing the default button image using 'element_create' (default is plus minus). This thing is called "indicator", it holds the images for this 'buttons'. Code should be simple then. 'image' is how folders looks like when closed (). 'user1' is when they are opened (). 'user2' is for an item in a folder (not a subfolder). I don't want it to show something, so I am using transparent image of size 1 pixel. Width and Height are to make indicator perfectly fit the image.

我还配置了Treeview.项,以便在左侧腾出额外的空间.不是真的需要.

最后,创建一个TreeView.因此,代码如下:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
style = ttk.Style()

# Images I will use (all have a size of 16 by 16 pixels):
images = {
    "folder_closed": tk.PhotoImage(file="folder1.png"),
    "empty": tk.PhotoImage(file="emptypixel.png"),
    "folder_opened": tk.PhotoImage(file="folder2.png")
}

"""
Treeview structure:
Treeview:   Treeview.field( Treeview.padding( Treeview.treearea))
Treeview.Heading:   Treeheading.cell; Treeheading.border( Treeheading.padding( Treeheading.image; Treeheading.text))
Treeview.Cell:   Treedata.padding( Treeitem.text)
Treeview.Item:   Treeitem.padding( Treeitem.indicator; Treeitem.image; Treeitem.text)
"""

style.element_create("Treeitem.indicator",
                     "image", images["folder_closed"],  # for a folder when it is 'closed'
                     ("user1", images['folder_opened']),  # when folder is opened
                     ("user2", images['empty']),  # for a file in a folder (not subfolder). By default same as folders
                     width=16, height=16)  # mine image sizes



style.configure('Treeview.Item', padding=(5, 0))  # may be omitted


# Create you tree!
treeview = ttk.Treeview(root, show='tree')

一个缺点是,这可能是有用的修复:该样式将用于所有的树视图在一个文件.这并不总是我们想要的.

complex solution:

我正在添加自定义样式.这是通过在物品和指示器上添加前缀‘Custom’(可以是任何名称)来实现的.这将继承默认的树元素."项目"的布局需要重新创建,因为应该添加新的指标,而不是默认的指标.所以代码是:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
style = ttk.Style()

# Images I will use (all have a size of 16 by 16 pixels):
images = {
    "folder_closed": tk.PhotoImage(file="folder1.png"),
    "empty": tk.PhotoImage(file="emptypixel.png"),
    "folder_opened": tk.PhotoImage(file="folder2.png")
}

# this is used to make extra scape from the icons of folders to the rest of the line.
imageToText_padx = 4

style.element_create("Custom.Treeview.indicator",
                     "image", images["folder_closed"],  # for a folder when it is 'closed'
                     ("user1", images['folder_opened']),  # when folder is opened
                     ("user2", images['empty']),  # for a file in a folder (not subfolder). By default same as folders
                     width=16 + imageToText_padx, height=16, sticky='wns')


style.layout("Custom.Treeview.Item", [
        ("Treeitem.padding", {"sticky": "nswe", "children":
            [
             ("Custom.Treeview.indicator", {"side": "left", "sticky": ""}),
             ("Treeitem.image", {"side": "left", "sticky": ""}),
             ("Treeitem.text", {"side": "left", "sticky": ""})
             ]
         })
    ])

style.configure('Custom.Treeview.Item', padding=(5, 0))  # may be omitted


# Apply the custom style to the treeview and create you tree!
treeview = ttk.Treeview(root, style='Custom.Treeview', show='tree')

这可以在一个程序中创建:

我还通过将"ELEMENT_CREATE"中的"Width"增加某个数字来增加文件夹图标和该行其余部分之间的间距.

总结:风格非常强大,如果你知道如何使用它们的话.这真的可以改变小工具的外观.不幸的是,它们没有得到很好的记录.

Python-3.x相关问答推荐

动态范围内来自另外两列的列求和

PySpark每毫秒使用先前的值填充数据

在REPLACE INTO中引用变量会抛出sqlite3.OperationalError

「Python Pandas」多级索引列和行匹配,如果列和行名称相似,则排除这些单元格中的值添加

TypeError: issubclass() arg 1 在 Flask 中导入 langchain 时必须是一个类

使用 Python 在特定组的列中设置上限

使用 multiprocessing 处理图像

来自嵌套字典的完整地址

如何使用 django rest 框架在 self forienkey 中删除多达 n 种类型的数据?

Pytorch:图像标签

使用 Python 解析 JSON 嵌套字典

无法在 macOS 上的 Anaconda3 python3.6 上安装 OpenCV3

如何禁用 pylint 禁止自用警告?

sys.stdin.readline() 和 input():读取输入行时哪个更快,为什么?

通过多个键对字典列表进行分组和聚合

在 Python 3 中获取所有超类

将 args、kwargs 传递给 run_in_executor

当默认 pip 为 pip2 时,升级 pip3 的正确格式是什么?

python asyncio add_done_callback 与 async def

有没有办法在多个线程中使用 asyncio.Queue ?