python tkinter 中的部分搜索不起作用

我的部分代码有问题。这是关于在树视图中进行部分搜索。我在这里找到了堆栈溢出部分搜索方法,并尝试与我的代码一起使用。它不起作用 - 它没有给出任何结果。下面的代码:


from tkinter import *

from tkinter import ttk

root = Tk()

sv = StringVar()

ids = []

names = []

def add():

    names = tree.insert("",END,values=(e0.get(),e1.get(),e2.get(),e3.get()))


for i in range(len(names)):

    ids.append(tree.insert("", "end", text=names[i]))

 

def command(*args):

    selections = []

    for i in range(len(names)):

        if entry.get() != "" and entry.get() == names[i][:len(entry.get())]:

            selections.append(ids[i])

        tree.selection_set(selections)

sv.trace("w", command)

entry = Entry(root, textvariable=sv,width=13)

entry.grid(row=2,column=1,rowspan=3,sticky=W)

e0 = Entry(root,width=15)

e0.grid(row=0,column=1,rowspan=1,sticky=W)

e1 = Entry(root,width=15)

e1.grid(row=0,column=1,rowspan=2,sticky=W)

e2 = Entry(root,width=15)

e2.grid(row=0,column=1,rowspan=3,sticky=W)

e3 = Entry(root,width=15)

e3.grid(row=0,column=1,rowspan=4,sticky=W)

 

btn1 = Button(root,text="add",width=5,command=add)

btn1.grid(row =0,column=0,rowspan=5)

lb1 = Label(root,text="serial num:")

lb1.grid(row =0,column=0,rowspan=1)

lb2 = Label(root,text="medicine\nname ")

lb2.grid(row =0,column=0,rowspan=2)

lb3 = Label(root,text="quatity")

lb3.grid(row =0,column=0,rowspan=3)

lb4 = Label(root,text="expiry Date")

lb4.grid(row =0,column=0,rowspan=4)

lb4 = Label(root,text="search box")

lb4.grid(row =1,column=0,rowspan=6)

#treeview

tree = ttk.Treeview(root,height=25)

tree["columns"]=("one","two","three","four")

tree.column("one",width=120)

tree.column("two",width=160)

tree.column("three",width=130)

tree.column("four",width=160)

tree.heading("one", text="Numer seryjny leku")

tree.heading("two", text="Nazwa Leku")

tree.heading("three", text="Ampułki/Tabletki")

tree.heading("four",text="Data ważności")

tree["show"]="headings"

tree.grid(row=0,column=2,rowspan=6,pady=20)

root.geometry("840x580")

root.mainloop()


隔江千里
浏览 75回答 1
1回答

慕容3067478

变量names未定义。您应该在代码的开头放置与以下行类似的行:names = []另一方面,您必须如下声明命令函数才能使其工作,因为跟踪回调需要至少三个参数:def command(*args):顺便说一句,如果您不想丢失 variable 中的数据names,我会以面向类的方式转换您的代码,例如以下代码:from tkinter import *from tkinter import ttkroot = Tk()sv = StringVar()ids = []class Tree():    def __init__(self, root):        self.names = []        sv.trace("w", self.command)        self.entry = Entry(root, textvariable=sv, width=13)        self.entry.grid(row=2,column=1,rowspan=3,sticky=W)        self.e0 = Entry(root,width=15)        self.e0.grid(row=0,column=1,rowspan=1,sticky=W)        self.e1 = Entry(root,width=15)        self.e1.grid(row=0,column=1,rowspan=2,sticky=W)        self.e2 = Entry(root,width=15)        self.e2.grid(row=0,column=1,rowspan=3,sticky=W)        self.e3 = Entry(root,width=15)        self.e3.grid(row=0,column=1,rowspan=4,sticky=W)                self.btn1 = Button(root,text="add",width=5,command=self.add)        self.btn1.grid(row =0,column=0,rowspan=5)        self.lb1 = Label(root,text="serial num:")        self.lb1.grid(row =0,column=0,rowspan=1)        self.lb2 = Label(root,text="medicine\nname ")        self.lb2.grid(row =0,column=0,rowspan=2)        self.lb3 = Label(root,text="quatity")        self.lb3.grid(row =0,column=0,rowspan=3)        self.lb4 = Label(root,text="expiry Date")        self.lb4.grid(row =0,column=0,rowspan=4)        self.lb4 = Label(root,text="search box")        self.lb4.grid(row =1,column=0,rowspan=6)        #treeview        self.tree = ttk.Treeview(root,height=25)        self.tree["columns"]=("one","two","three","four")        self.tree.column("one",width=120)        self.tree.column("two",width=160)        self.tree.column("three",width=130)        self.tree.column("four",width=160)        self.tree.heading("one", text="Numer seryjny leku")        self.tree.heading("two", text="Nazwa Leku")        self.tree.heading("three", text="Ampułki/Tabletki")        self.tree.heading("four",text="Data ważności")        self.tree["show"]="headings"        self.tree.grid(row=0,column=2,rowspan=6,pady=20)        def add(self):        self.names = self.tree.insert("",END,values=(self.e0.get(),self.e1.get(),self.e2.get(),self.e3.get()))        for i in range(len(self.names)):            ids.append(self.tree.insert("", "end", text=self.names[i]))        def command(self, *args):        selections = []        for i in range(len(self.names)):            if self.entry.get() != "" and self.entry.get() == self.names[i][:len(self.entry.get())]:                selections.append(ids[i])            self.tree.selection_set(selections)tree = Tree(root)root.geometry("840x580")root.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python