Reputation: 55
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()
Upvotes: 1
Views: 1575
Reputation: 61
The ttk Style allows for changing the layout of all widgets. If you 'simply' remove the indicator from the layout, the indentation disappears.
s = ttk.Style()
s.layout('my.Treeview.Item',
[('Treeitem.padding', {'sticky': 'nswe', 'children': [
# The next line is removed from the layout, removing the indicator
# ('Treeitem.indicator', {'side': 'left', 'sticky': ''}),
('Treeitem.image', {'side': 'left', 'sticky': ''}),
('Treeitem.focus', {'side': 'left', 'sticky': '', 'children': [
('Treeitem.text', {'side': 'left', 'sticky': ''})
]})
]})
]
)
# Use your customized style for the Treeview widget
self.tree = ttk.Treeview(master, style='my.Treeview')
Upvotes: 1
Reputation: 21
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.
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.
s = ttk.Style()
s.theme_use('default')
s.configure('Treeview.Item', indicatorsize=0)
This approach seems to only work for the
default
,clam
andclassic
themes on Windows 10. For other themes, Ron's answer may be your best bet.
Upvotes: 2
Reputation: 2690
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])
Upvotes: 3