简体   繁体   English

如何删除小部件 Tkinter Treeview 上的缩进?

[英]How to remove indent left on the widget Tkinter Treeview?

Can I somehow remove this indentation?我可以以某种方式删除此缩进吗? I know that I can remove the first column, but then I can not insert the image.我知道我可以删除第一列,但是我无法插入图像。树视图图像

import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk

class App:
    def __init__(self, master):
        self.tree = ttk.Treeview(master)
        self.tree.heading('#0', text='Directory', anchor='w')
        self.tree["columns"]=("num_files")
        self.tree.heading('num_files', text='Number of files', anchor='w')

        self.image_tk = ImageTk.PhotoImage(Image.open('icon.png'))

        for i in range(10):
            self.tree.insert('', 'end', text="Dir %d" % (i+1), values=(15), open=False, image=self.image_tk)
        self.tree.pack(expand=1,fill="both")

root = tk.Tk()
app = App(root)
root.mainloop()

Assuming the indentation you're referring to is the left side of the first column (left of all the icons), you can adjust the entire widget padding as needed.假设您所指的缩进是第一列的左侧(所有图标的左侧),您可以根据需要调整整个小部件的填充。 For your application, start with:对于您的应用程序,请从以下内容开始:

self.tree = ttk.Treeview(master, padding=[-15,0,0,0])

What you have highlighted in red is the area in which the Treeview's indicator resides -- the open or close toggle icon -- that is shown if any of the Item objects contain subitems of their own.您以红色突出显示的是 Treeview 指示器所在的区域——打开关闭切换图标——如果任何Item对象包含它们自己的子项,就会显示该区域。

One way to accomplish removing that area would be to just remove the indicator altogether while using a ttk theme that allows you to do so.完成删除该区域的一种方法是在使用允许您这样做的 ttk 主题的同时完全删除指示器。

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

This approach seems to only work for the default , clam and classic themes on Windows 10. For other themes, Ron's answer may be your best bet.这种方法似乎只适用于 Windows 10 上的defaultclamclassic主题。对于其他主题,Ron 的答案可能是您最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM