是否可以使用列表来存储列表框数据,Tkinter
is it Possible to use Lists to Store Listbox data , 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(例如组合框)中查看这些值取决于您。
with open('List.txt') as inFile:
ListValues = [line for line in inFile]
ListValues = sorted(ListValues)
我想将它存储在一个列表中,所以当我关闭程序并运行它回来时,它会在那里。
我知道这段代码是错误的,但我找不到存储 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(例如组合框)中查看这些值取决于您。
with open('List.txt') as inFile:
ListValues = [line for line in inFile]
ListValues = sorted(ListValues)