我想在我的树形视图中添加过滤器按钮。 我有两个栏目,例如“汽车”和“维修”。 我想在输入框car中使用Car name和textchanged事件进行过滤,并且将维修状态过滤掉。
您是否具有类似的功能。有人可以帮助我吗?
我尝试使用tree.get_children(item)方法,但尚未成功
from tkinter import *
import tkinter as tk
import tkinter.font as tkFont
import tkinter.ttk as ttk
class MultiColumnListbox():
"""use a ttk.TreeView as a multicolumn ListBox"""
def __init__(self,root,elementheader):
self.tree = ''
self._root = root
self.element_header= elementheader
self._setup_widgets()
def _setup_widgets(self):
win2 = Toplevel(self._root)
win2.geometry('650x250')
win2.resizable(width=0, height=0)
message = "Read General"
Label(win2, text=message).pack()
new_element_header = [self.element_header[0], self.element_header[1]]
self.tree = ttk.Treeview(win2, columns=new_element_header, show="headings", selectmode='browse')
self.tree.pack(side='left')
self.entry = tk.Entry(win2,bd=10)
self.entry.pack()
self.button = tk.Button(win2,text = 'Search',command = self.search).pack()
self.tree.heading(self.element_header[0], text=self.element_header[0])
self.tree.heading(self.element_header[1], text=self.element_header[1])
vsb = ttk.Scrollbar(win2,orient="vertical",
command=self.tree.yview)
vsb.pack(side='left', fill='y')
hsb = ttk.Scrollbar(win2,orient="horizontal",
command=self.tree.xview)
self.tree.configure(yscrollcommand=vsb.set,
xscrollcommand=hsb.set)
def search(self,item = ''):
print("hihihi")
children = self.tree.get_children(item)
for child in children:
text = self.tree.item(child,'text')
if text.startswith(self.entry.get()):
self.tree.selection_set(child)
print("Hi1")
return True
else:
res = self.search(child)
print("child:",child)
print("child:", res)
print("Hi2")
if res:
return True
def _build_tree(self,element_list):
print("I am here")
self.tree.delete(*self.tree.get_children())
for col in self.element_header:
self.tree.heading(col, text=col.title(),
command=lambda c=col: treeview_sort_column(self.tree,c,False))
# adjust the column's width to the header string
self.tree.column(col,
width=tkFont.Font().measure(col.title()))
for item in element_list:
# children = self.tree.get_children(item)
# print(children)
self.tree.insert('', 'end', values=item)
# for child in children:
# if child == item :
# exit()
# else:
# self.tree.insert('', 'end', values=item)
self.tree.insert('', 'end', values=item)
# adjust column's width if necessary to fit each value
for ix, val in enumerate(item):
col_w = tkFont.Font().measure(val)
if self.tree.column(self.element_header[ix],width=None)<col_w:
self.tree.column(self.element_header[ix], width=col_w)
def treeview_sort_column(tv, col, reverse):
l = [(tv.set(k, col), k) for k in tv.get_children('')]
l.sort(reverse=reverse)
# rearrange items in sorted positions
for index, (val, k) in enumerate(l):
tv.move(k, '', index)
# reverse sort next time
tv.heading(col, command=lambda: \
treeview_sort_column(tv, col, not reverse))
def sortby(tree, col, descending):
data = [(tree.set(child, col), child) \
for child in tree.get_children('')]
data.sort(reverse=descending)
for ix, item in enumerate(data):
tree.move(item[1], '', ix)
# switch the heading so it will sort in the opposite direction
tree.heading(col, command=lambda col=col: sortby(tree, col, \
int(not descending)))
# the test data ...
if __name__ == '__main__':
element_header= ['car', 'repair']
element_list = [
('Hyundai', 'brakes'),
('Honda', 'light'),
('Lexus', 'battery'),
('Benz', 'wiper'),
('Ford', 'tire'),
('Chevy', 'air'),
('Chrysler', 'piston'),
('Toyota', 'brake pedal'),
('BMW', 'seat')
]
root = Tk()
listbox = MultiColumnListbox(root,element_header)
listbox._build_tree(element_list)
root.mainloop()