如何更新 tkinter 中顶层 window 中的条目小部件条目?

How can I update entry widget entries in a toplevel window in tkinter?

我想做的是从根 window 打开一个顶层 window,其中我有一系列条目小部件,修改条目并关闭 window。我在其中一篇文章中找到了一段代码,并对其进行了修改以满足我的需要。该代码仅在我第一次打开顶层 window 时有效,但之后它会打开没有输入字段的顶层 window!我不明白发生了什么!有人可以帮忙吗?我对 python 很陌生。这是代码:

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()

这是至少我一直在寻找的解决方案。

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()