[英]Tkinter TreeView remove dotbox highlight
Hi I'm trying to remove the dotbox hightlight when i select a certain row of my TreeView, It highlights the text of the first columns that also contains a image: 嗨,当我选择TreeView的某一行时,我试图删除点状框高光,它突出显示了第一列的文本,其中也包含图像:
I'm a newbie, but guessing that is happening because the first column is not created with the constructor and is separate to the other columns as you can see in the following example: 我是一个新手,但是正在猜测是因为第一列不是使用构造函数创建的,并且与其他列是分开的,如以下示例所示:
f = ttk.Frame(master)
f.pack(fill=BOTH, expand=True)
self.dataCols = ('Project Name','Status', 'Cores', 'Added date/time')
self.tree = ttk.Treeview(columns=self.dataCols)
self.tree.grid(in_=f, row=0, column=0, sticky=NSEW)
self.tree.heading('#0', text='', anchor='center')
self.tree.heading('#1', text='Project Name', anchor='center')
self.tree.heading('#2', text='Status', anchor='center')
self.tree.heading('#3', text='Cores', anchor='center')
self.tree.heading('#4', text='Added date/time', anchor='center')
self.tree.column('#0', anchor='center', width=1)
self.tree.column('#1', anchor='w')
self.tree.column('#2', anchor='center')
self.tree.column('#3', anchor='center')
self.tree.column('#4', anchor='center')
f.rowconfigure(0, weight=1)
f.columnconfigure(0, weight=1)
style = ttk.Style(master)
style.configure('Treeview', rowheight=38)
It was implemented this way to allow a image as a column value. 以这种方式实现,以允许将图像作为列值。
Edit : 编辑 :
This is how I'm inserting rows to the Treeview. 这就是我在Treeview中插入行的方式。
self.tree.insert('', 'end', image=auxiliary_classes.global_data.img_container[obj.name],
value=obj.get_list())
Edit: 编辑:
style = ttk.Style(master)
style.theme_use('clam')
style.layout('nodotbox.Treeview.Item',
[('Treeitem.padding',
{'children': [('Treeitem.indicator', {'side': 'left', 'sticky': ''}),
('Treeitem.image', {'side': 'left', 'sticky': ''}),
('Treeitem.text', {'side': 'left', 'sticky': ''})],
'sticky': 'nswe'})])
style.configure(style='nodotbox.Treeview')
Edit: 编辑:
This code did the trick! 这段代码成功了!
style = ttk.Style()
style.layout("Treeview.Item",
[('Treeitem.padding', {'sticky': 'nswe', 'children':
[('Treeitem.indicator', {'side': 'left', 'sticky': ''}),
('Treeitem.image', {'side': 'left', 'sticky': ''}),
# ('Treeitem.focus', {'side': 'left', 'sticky': '', 'children': [
('Treeitem.text', {'side': 'left', 'sticky': ''}),
# ]})
],
})]
)
The dotbox highlight is part of the style so you can remove it by changing the layout of the treeview's style. 点框突出显示是样式的一部分,因此您可以通过更改树视图样式的布局来将其删除。
The default layout is 默认布局是
[('Treeitem.padding',
{'children': [('Treeitem.indicator', {'side': 'left', 'sticky': ''}),
('Treeitem.image', {'side': 'left', 'sticky': ''}),
('Treeitem.focus',
{'children': [('Treeitem.text', {'side': 'left', 'sticky': ''})],
'side': 'left',
'sticky': ''})],
'sticky': 'nswe'})]
and the dotbox is 'Treeitem.focus'
, so if you create a custom layout without it, then it will no longer be visible: 并且该点框是'Treeitem.focus'
,因此,如果您创建一个没有它的自定义布局,则它将不再可见:
style = ttk.Style(master)
style.layout('nodotbox.Treeview.Item',
[('Treeitem.padding',
{'children': [('Treeitem.indicator', {'side': 'left', 'sticky': ''}),
('Treeitem.image', {'side': 'left', 'sticky': ''}),
('Treeitem.text', {'side': 'left', 'sticky': ''})],
'sticky': 'nswe'})])
tree.configure(style='nodotbox.Treeview')
EDIT : Full example: 编辑 :完整示例:
import tkinter as tk
from tkinter import ttk
master = tk.Tk()
style = ttk.Style(master)
style.layout('nodotbox.Treeview.Item',
[('Treeitem.padding',
{'children': [('Treeitem.indicator', {'side': 'left', 'sticky': ''}),
('Treeitem.image', {'side': 'left', 'sticky': ''}),
('Treeitem.text', {'side': 'left', 'sticky': ''})],
'sticky': 'nswe'})])
tk.Label(master, text='Default layout').pack()
tree1 = ttk.Treeview(master, columns=('a', 'b'))
tree1.insert('', 'end', text='item 1', values=('a', 'b'))
tree1.insert('', 'end', text='item 2', values=('a', 'b'))
tree1.pack()
tk.Label(master, text='No dotbox').pack()
tree2 = ttk.Treeview(master, style='nodotbox.Treeview', columns=('a', 'b'))
tree2.insert('', 'end', text='item 1', values=('a', 'b'))
tree2.insert('', 'end', text='item 2', values=('a', 'b'))
tree2.pack()
master.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.