簡體   English   中英

如何更新 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM