python - 更新 Tkinter 列表框而不退出并重新进入?

标签 python listbox tkinter

这里是 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/

相关文章:

python - 阻止 tkinter 框架被推出屏幕

python - 为什么我的按钮不适用于 Tkinter 计算器?

python - OO Black Jack Game - 计算手牌值(value)

python - python3中的编码/解码(Berkeley数据库记录)

vb.net - 如何使用所有打开表单的列表填充列表框

Python Tkinter 列表框解除默认选择的绑定(bind)

python - Pandas 过滤数据框中的值

将中文字符从 Oracle 导入 json.dump 时抛出 Python UnicodeDecodeError

c# - 无法将类型 'System.Linq.IQueryable<>' 隐式转换为 'Model' 。存在显式转换(是否缺少强制转换?)

python - 为什么 delete(0, tk.END) 在我的条目中留下一个字符?