我希望我的 Treeview 在使用 tkinter 中的条目进行过滤时显示条纹行标签。该代码在过滤之前工作正常,过滤功能也工作正常,但它不显示条纹行标签。有人可以帮助解决它。我认为基于 Python 和 tkinter 的知识水平,那里一定是无用的代码。这是代码:
问题出在 defexecute_search(event) 函数上:
#import modules
from tkinter import *
from tkinter import ttk
#Load data
Data = [
["John", "Elder", 1, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Johny", "Elder", 2, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Joe", "Elder", 3, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["marty", "Elder", 4, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["don", "Elder", 5, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["mike", "Elder", 6, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Paula", "Elder", 7, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Vianney", "Elder", 8, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Mariana", "Elder", 9, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Ignacio", "Elder", 10, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Paul", "Elder", 11, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Pablo", "Elder", 12, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Pedro", "Elder", 13, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Pascal", "Elder", 14, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
["Peter", "Elder", 15, "3223 NE 36Th", "Fort Lauderdale", "Florida", "33308"],
]
#create a window
root = Tk()
root.title('Users List')
#root.iconbitmap()
root.geometry("1000x500")
#Add some style
style = ttk.Style()
#Pick a Theme
style.theme_use('default')
#Configure the Treeview color
style.configure("Treeview",
bakground="#D3D3D3",
FOREGROUND="black",
rowheight=25,
fieldbackground="#D3D3D3")
#Change selected color
style.map('Treeview',
bakground=[('selected', "#D347083")])
#Create the Treeview Frame
tree_frame = Frame(root)
tree_frame.pack(pady=10)
#Create the Treeview Scrollbar
tree_scroll = Scrollbar(tree_frame)
tree_scroll.pack(side=RIGHT, fill=Y)
#Create the Treeview
my_tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set, selectmode="extended")
my_tree.pack()
#Configure Scrollbar
tree_scroll.config(command=my_tree.yview)
#Defining Columns
my_tree['columns'] = ("First Name", "Last Name", "ID", "Address", "City", "State", "Zipcode")
#Formating our columns
my_tree.column("#0", width=0, stretch=NO)
my_tree.column("First Name", anchor=W, width=140)
my_tree.column("Last Name", anchor=W, width=140)
my_tree.column("ID", anchor=CENTER, width=100)
my_tree.column("Address", anchor=CENTER, width=140)
my_tree.column("City", anchor=CENTER, width=140)
my_tree.column("State", anchor=CENTER, width=140)
my_tree.column("Zipcode", anchor=CENTER, width=140)
#Creating headings
my_tree.heading("#0", text="", anchor=W)
my_tree.heading("First Name", text="First Name", anchor=W)
my_tree.heading("Last Name", text="Last Name", anchor=W)
my_tree.heading("ID", text="ID", anchor=CENTER)
my_tree.heading("Address", text="Address", anchor=CENTER)
my_tree.heading("City", text="City", anchor=CENTER)
my_tree.heading("State", text="State", anchor=CENTER)
my_tree.heading("Zipcode", text="Zipcode", anchor=CENTER)
#Add our data to the screen
global count
count = 0
for record in Data:
if count % 2 == 0:
my_tree.insert(parent='', index='end', iid=count, text='', values=(record[0],
record[1], record[2], record[3], record[4], record[5], record[6]), tags=
('evenrow',))
else:
my_tree.insert(parent='', index='end', iid=count, text='', values=(record[0],
record[1], record[2], record[3], record[4], record[5], record[6]), tags=
('oddrow',))
#increment counter
count +=1
#print(record)
#create a frame
data_frame = LabelFrame(root, text="Record")
data_frame.pack(fill="x", expand="yes", padx=20)
#create the entry
fn_label = Label(data_frame, text="First Name")
fn_label.grid(row=0, column=0, padx=10)
fn_entry = Entry(data_frame)
fn_entry.grid(row=0, column=1, padx=10)
#function to filter data
def execute_search(event):
global count
count = 0
query = fn_entry.get().lower()
print(query)
for item in my_tree.get_children():
my_tree.delete(item)
data_list = []
for item in Data:
if query in item[0].lower():
my_tree.insert("", "end", values=item)
for item in Data:
data_list.append(item)
#Create straped row tags
my_tree.tag_configure('oddrow', background="white")
my_tree.tag_configure('evenrow', background="lightblue")
#Not in use
def query_database():
global count
count = 0
for record in Data:
if count % 2 == 0:
my_tree.insert(parent='', index='end', iid=count, text='', values=
(record[0], record[1], record[2], record[3], record[4], record[5],
record[6]), tags=('evenrow',))
else:
my_tree.insert(parent='', index='end', iid=count, text='', values=
(record[0],
record[1], record[2], record[3], record[4], record[5], record[6]),
tags=('oddrow',))
#increment counter
count +=1
#print(record)
fn_entry.bind('<KeyRelease>', execute_search)
root.mainloop()
在 execute_search()
中插入数据时,您没有指定
tag选项。
更新
execute_search()
以解决问题:
def execute_search(event):
global count
count = 0
my_tree.delete(*my_tree.get_children())
query = fn_entry.get().lower()
for item in Data:
if query in item[0].lower():
tag = 'evenrow' if (count%2 == 0) else 'oddrow'
my_tree.insert("", "end", values=item, tags=(tag,))
count += 1
请注意,我已经删除了与
data_list
相关的内容,因为看起来没有必要。