这里是 Tkinter 初学者,也是一般的 Python 菜鸟。
我有一个程序,可以将项目附加到列表中并在列表框中显示该列表。重新加载时它将正确显示所有项目,但我希望新值在列表框中可见而无需重新加载。这是我的尝试。它不会返回任何错误,但也不起作用。
我在 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.delete(0, END)
for i in range(len(new_list)):
listbox.insert(END,new_list[i][0])
这将删除框中当前的所有内容并放入新列表。
希望这对您有所帮助,如果您有任何疑问,请发表评论。
关于python - 更新 Tkinter 列表框而不退出并重新进入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22720046/