嘿,我已经在 python 中成功创建了一个 tkinter GUI,它将输入的值保存在一个文本文件中。这是代码:
from Tkinter import *
root = Tk()
def save():
open("text.txt","w").close()
text = e.get() + "\n" + e1.get() + "\n" + e2.get() + "\n"
with open("text.txt", "a") as f:
f.write(text)
w1 = Label(root, text="Controller value")
w1.pack()
e = Entry(root)
e.pack()
w2 = Label(root, text="Velocity")
w2.pack()
e1 = Entry(root)
e1.pack()
w3 = Label(root, text="Desired Heading")
w3.pack()
e2 = Entry(root)
e2.pack()
toolbar = Frame(root)
b = Button(toolbar, text="save", width=9, command=save)
b.pack(side=LEFT, padx=2, pady=2)
toolbar.pack(side=TOP, fill=X)
mainloop()
现在我要做的是在 GUI 中创建 3 个新的文本框来显示文件的内容。例如我的 text.txt 文件有以下内容:
3
2
4
现在我希望这 3 个值中的每一个都显示在 GUI 的 3 个文本框中。基本上我希望 GUI 中的第一个文本框显示 3,第二个文本框 2 和第三个文本框 4。请帮帮我。
请您参考如下方法:
像这样? 更改仅在主要功能中进行,因此您只需更改 1。
def main():
root = Tk()
c = Canvas(root,width=600)
c.pack(side = 'left',expand=1,fill=BOTH)
c2 = Canvas(c,width=600)
c2.pack(side = 'left',expand=1,fill=BOTH)
c3 = Canvas(c,width=600)
c3.pack(side = 'left',expand=1,fill=BOTH)
w1 = Label(c2, text="Controller value")
w1.pack()
e = Entry(c2)
e.pack()
w2 = Label(c2, text="Velocity")
w2.pack()
e1 = Entry(c2)
e1.pack()
w3 = Label(c2, text="Desired Heading")
w3.pack()
e2 = Entry(c2)
e2.pack()
toolbar = Frame(c2)
b = Button(toolbar, text="save", width=9, command=lambda:save(e,e1,e2))
b.pack(side='left', padx=2, pady=2)
toolbar.pack(side=TOP, fill=X)
lt = Label(c3,text='Controller')
lt.pack(side='top',expand=1,fill='x')
l = Label(c3,text='',bg='red')
l.pack(side='top',expand=1,fill='x')
lt2 = Label(c3,text='Velocity')
lt2.pack(side='top',expand=1,fill='x')
l2 = Label(c3,text='',bg='yellow')
l2.pack(side='top',expand=1,fill='x')
lt3 = Label(c3,text='Desired Heading')
lt3.pack(side='top',expand=1,fill='x')
l3 = Label(c3,text='',bg='blue')
l3.pack(side='top',expand=1,fill='x')
b2 = Button(c3,text='load',command=lambda:loadme(l,l2,l3))
b2.pack(fill='x', padx=2, pady=2)
root.mainloop()
