python - 是否可以使用列表来存储列表框数据,Tkinter
问题描述
我想将它存储在一个列表中,所以当我关闭程序并运行它时,它会在那里。
我知道这段代码是错误的,但我找不到存储Listbox
数据的方法
from tkinter import *
ex = []
def insert() :
ex.append(entry.get())
for i in ex :
listbox.insert(listbox.size(),ex)
window= Tk()
listbox= Listbox(window)
listbox.pack()
button = Button(window,text="insert",command=insert)
button.pack()
entry= Entry(window,width=15)
entry.pack()
window.mainloop()
解决方案
我认为以下代码可以满足您将条目值保存为列表的需求。
from tkinter import Entry, Tk, Button, StringVar
Root = Tk()
EntryValue = StringVar()
file = open ('List.txt', 'w+')
def insert():
with open ('List.txt', 'a') as file:
file.write('\n'+ EntryValue.get())
file.close
class GUI:
def __init__(self, master):
self.master = master
master.bind("<Escape>", lambda x : Root.destroy())
master.entry = Entry(Root, textvariable=EntryValue)
master.entry.pack()
master.button = Button(Root,text="insert",command=insert)
master.button.pack()
def main():
GUI(Root)
Root.mainloop()
main()
以下代码将用于从 .txt 文件中读取值,但您希望如何在 UI 中查看这些值取决于您,例如 Combobox。
with open('List.txt') as inFile:
ListValues = [line for line in inFile]
ListValues = sorted(ListValues)
推荐阅读
- python - 我正在研究 python django 并有一个注册表单有诸如用户名电子邮件密码之类的字段要检查是否存在
- python - Django Oscar 图像保存在缓存文件夹中但未显示
- mysql - 在 MySQL 中执行递归查询时出错
- c# - 使用 Microsoft.Management.Infrastructure 检索串行端口信息
- css - 如何摆脱图标的边框?
- python - 在 python 中运行进程时做某事
- android - firebase 存储 java.lang.IllegalStateException:任务尚未完成
- android - Firebase 回收器选项不起作用:实时数据库
- html - 标题上的背景图像有问题
- c++ - 如何在 c++ winform 中的线程之间传递数据?