Tkinter初学者在这里,和一般的蟒蛇noob。更新Tkinter列表框没有退出并重新进入?
我有一个程序,将项目附加到列表并在列表框中显示该列表。它将在重新加载时正确显示所有项目,但我希望新值在列表框中可见而不必重新加载。这是我的尝试。它不会返回任何错误,但它也不起作用。
我在Mac OSX上运行2.7。请记住,我对这个东西相当陌生。
def OnPressEnter(self,event):
hold = self.entryVariable.get()
hold = str(hold)
with open('GoalTrak/StudentList.txt', 'a') as textfile: #This appends students to the already existing document of students
if (hold) not in student_list_temp:
student_list_temp.append(hold[:-1])
textfile.write(hold + '\n')
self.labelVariable.set(self.entryVariable.get() + " added")
self.StudentListDisplay.insert(Tkinter.END,hold[:-1])
else:
self.labelVariable.set(self.entryVariable.get() + " is already in list")
textfile.close()
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
在此先感谢!
编辑:这是我在其中定义我的列表框代码:
self.StudentListDisplay = Tkinter.Listbox(self)
self.StudentListDisplay.grid(row=2,column=0,columnspan=2,sticky='W')
for student in student_list_temp:
self.StudentListDisplay.insert(Tkinter.END, student)
你能告诉我们你定义你的ListBox的代码吗? –
@AlexThornton更新! –
你是什么意思“无需重新加载”? – mgilson