栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在树形视图中搜索并突出显示/选择包含所搜索项目的行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在树形视图中搜索并突出显示/选择包含所搜索项目的行

请在下面查看我的解释性代码段:

from tkinter import *from tkinter import ttkclass App:    def __init__(self, root):        self.root = root        self.tree = ttk.Treeview(self.root) #create tree        self.sv = StringVar() #create stringvar for entry widget        self.sv.trace("w", self.command) #callback if stringvar is updated        self.entry = Entry(self.root, textvariable=self.sv) #create entry        self.names = ["Jane", "Janet", "James", "Jamie"] #these are just test inputs for the tree        self.ids = [] #creates a list to store the ids of each entry in the tree        for i in range(len(self.names)): #creates an entry in the tree for each element of the list #then stores the id of the tree in the self.ids list self.ids.append(self.tree.insert("", "end", text=self.names[i]))        self.tree.pack()        self.entry.pack()    def command(self, *args):        self.selections = [] #list of ids of matching tree entries        for i in range(len(self.names)): #the below if check checks if the value of the entry matches the first characters of each element #in the names list up to the length of the value of the entry widget if self.entry.get() != "" and self.entry.get() == self.names[i][:len(self.entry.get())]:     self.selections.append(self.ids[i]) #if it matches it appends the id to the selections list        self.tree.selection_set(self.selections) #we then select every id in the listroot = Tk()App(root)root.mainloop()

因此,每次

entry
更新窗口小部件时,我们都会循环浏览名称列表,并检查
entry
窗口小部件的值是否与
element
in中的值匹配,
names

list
直到不超过窗口小部件的值的长度
entry
(例如,如果输入5个字符长的字符串,然后我们对照该元素的前5个字符进行检查)。

如果它们匹配

id
,则将树条目的附加到
list

检查完所有名称后,我们将

list
匹配
id
的传递给
self.tree.selection_set()
,然后突出显示所有匹配的树条目。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/623655.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号