【发布时间】:2014-05-08 08:10:42
【问题描述】:
这里是 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)
【问题讨论】:
-
你能告诉我们你定义列表框的代码吗?
-
@AlexThornton 更新了!
-
“无需重新加载”是什么意思?
-
@mgilson 列表框显示更新的唯一方法是我退出程序并重新启动它。
-
你怎么知道它不起作用?您是否验证了正在调用将变量添加到列表的代码?当然可以随时在列表框中插入项目,所以如果它不起作用,也许该代码永远不会被调用?