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

拟墨画扇 提交于 2020-05-17 06:03:55

问题


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

来源:https://stackoverflow.com/questions/61775964/how-can-i-update-entry-widget-entries-in-a-toplevel-window-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!