我正在忙着完成一项学校作业,而且我已经陷入了这段代码,任何帮助都会非常感激。 请参阅以下代码:
from tkinter import *
student_list = [['Tom','Information Systems'],['John','Computers'],['Johannes','Information Systems']]
class App(Frame):
def __init__(self,parent=None,**kw):
Frame.__init__(self,master=parent,**kw)
self.searchValue = StringVar()
self.searchBox = Entry(self,textvariable=self.searchValue)
self.searchBox.pack()
self.resultList = Listbox(self)
self.resultList.pack()
self.searchBox.bind('<Return>',self.update)
def update(self,e):
print("*")
self.resultList.delete(0,END)
searchkey = self.searchValue.get()
for student in student_list:
if searchkey == student[0]:
self.resultList.insert(END,str(student))
elif searchkey == student[1]:
self.resultList.insert(END,str(student))
if __name__ == '__main__':
root = Tk()
app = App(root)
app.pack()
root.mainloop()
是否可以为每个用户条目或任何替代方法创建新列表?
答案 0 :(得分:0)
这不太好但是有效。
from tkinter import *
student_list = [['Tom','Information Systems'],['John','Computers']]
class App(Frame):
def __init__(self,parent=None,**kw):
Frame.__init__(self,master=parent,**kw)
self.searchValue = StringVar()
self.searchBox = Entry(self,textvariable=self.searchValue)
self.searchBox.pack()
self.resultList = Listbox(self)
self.resultList.pack()
Label(self,text="Name:").pack()
self.Name = Entry(self)
self.Name.pack()
Label(self,text="Course:").pack()
self.Course = Entry(self)
self.Course.pack()
self.btnAdd = Button(self,text="Add",command=self.add_student)
self.btnAdd.pack()
self.searchBox.bind('<Return>',self.update)
def add_student(self):
global student_list
print(student_list)
name = self.Name.get()
course = self.Course.get()
student_list.append([name,course])
print(student_list)
def update(self,e):
print("*")
self.resultList.delete(0,END)
searchkey = self.searchValue.get()
for student in student_list:
if searchkey == student[0]:
self.resultList.insert(END,str(student))
elif searchkey == student[1]:
self.resultList.insert(END,str(student))
if __name__ == '__main__':
root = Tk()
app = App(root)
app.pack()
root.mainloop()
现在尝试自己解决下一个问题......到目前为止,我已经为你完成了两个步骤。