如何在tkinter的顶级窗口中更新条目窗口小部件条目? [英] How can I update entry widget entries in a toplevel window in tkinter?
问题描述
我想要做的是从根窗口打开一个顶层窗口,在该窗口中我有一系列条目小部件,修改条目并关闭该窗口.我在其中一个帖子中找到了一个代码,并对其进行了修改以满足我的需要.该代码仅在我第一次打开顶级窗口时起作用,但是此后,它将打开没有输入字段的顶级窗口!我不明白发生了什么!有人可以帮忙吗?我是python的新手.这是代码:
what I want to do, is to open from the root window a toplevel window in which I have series of entry widgets, modify the entries and close the window. I found a code in one of the posts and modified it to fit my need. The code works only the first time I open the toplevel window, but after that it opens the toplevel window without the entry fields! I don't understand what is happening!! Can anyone help please? I am quite new to python. Here is the code:
from tkinter import *
root = Tk()
entry_list = []
def openwindow():
window = Toplevel()
window.title("Data")
entry_list.clear()
for i in range(10):
entry_list.append(Entry(window))
entry_list[i].grid()
def update_entry_fields():
for i in entry_list:
i.delete(END)
i.insert(0, "")
print(float(entry_list[0].get()))
def closewindow():
window.withdraw()
savebtn = Button(window, text="Save & print", command = update_entry_fields)
closebtn = Button(window, text="Close", command=closewindow)
savebtn.grid()
closebtn.grid()
def printout():
print(float(entry_list[0].get()))
printbtn = Button(root, text="Test print", command = printout)
printbtn.grid()
openbutton = Button(root, text="open data sheet", command=openwindow)
openbutton.grid()
root.mainloop()
推荐答案
这里至少是我正在寻找的解决方案.
Here is the solution that at least i was looking for.
from tkinter import *
root = Tk()
entry_list = []
p = [5, 6, 7, 8, 9]
def openwindow():
window = Toplevel()
window.title("Data")
entry_list.clear()
for i in range(5):
v = DoubleVar()
entry_list.append(Entry(window, textvariable=v))
entry_list[i].grid()
v.set(p[i]) # set default entry values
def update_entry_fields():
for i in range(5):
p[i]=entry_list[i].get() # overwrite the entry
# test print from inside
print(p[i])
window.withdraw()
savebtn = Button(window, text="Save & close", command = update_entry_fields)
savebtn.grid()
window.mainloop()
# Test print from outside of function
def printout():
print(p[0])
printbtn = Button(root, text="Test print", command = printout)
printbtn.grid()
openbutton = Button(root, text="open data sheet", command=openwindow)
openbutton.grid()
#
root.mainloop()
这篇关于如何在tkinter的顶级窗口中更新条目窗口小部件条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!