[英]How to save a data after closing a window?
我試圖做一些GUI上Python3
與tkinter
。 到目前為止,我有主窗口和'Test'
按鈕,它打開第二個窗口。 第二個窗口有輸入、標簽、保存和關閉按鈕。 當您在條目中輸入內容並按save
按鈕時,標簽會顯示您在條目中輸入的文本。 但是關閉此窗口並再次打開后,標簽什么也不顯示。 如何讓這個標簽顯示關閉前最后一次輸入的文本? 例如,我在輸入中輸入“ Hi
”,按“ Save
”,然后按“ Close
”,然后我再次打開這個窗口,標簽顯示“ Hi
”
import tkinter as tk
def save_data(entry, t):
t.config(text = entry.get())
def close_action(current_window):
current_window.destroy()
def insertMainInfo():
new_window = tk.Tk()
new_window.geometry("307x131")
new_window.title("TestWindow")
test_entry = tk.Entry(new_window)
test_entry.place(relx = 0.283, rely = 0.1, height = 24, width = 127)
text = tk.Label(new_window)
text.place(relx = 0.283, rely = 0.25, height = 24, width = 127)
save_button = tk.Button(new_window, command = lambda: save_data(test_entry, text))
save_button.place(relx=0.283, rely=0.45, height=24, width=127)
save_button.configure(text = "Save")
close = tk.Button(new_window, command = lambda: close_action(new_window))
close.place(relx=0.283, rely=0.687, height=24, width=127)
close.configure(text = "Close")
new_window.mainloop()
if __name__ == '__main__':
top = tk.Tk()
top.geometry("307x131+557+330")
top.resizable(width=False, height=False)
top.title("MainWindow")
new_window_button = tk.Button(top, command = insertMainInfo)
new_window_button.place(relx=0.283, rely=0.687, height=24, width=127)
new_window_button.configure(text = "Test")
main_label = tk.Label(top)
main_label.place(relx=0.033, rely=0.153, height=41, width=284)
main_label.configure(text = "TestLabel")
top.mainloop()
我必須承認,你的問題標題有點含糊。 如果您只想更新最后一個條目的標簽,這里有一種修改代碼的簡單方法。
誠如,是一個很好的做法,我們只有一個Tk()
程序中,其他新窗口或彈出窗口應該使用Toplevel()
的tkinter
類; 所以我在你的insertMainInfo()
函數中使用它。
這里的重點是定義一個變量,我稱之為last_entry並且最初是空的或''。 將此作為主程序中new_window按鈕中的參數(在if __name__ == '__main__':
)到此變量(我還在此處添加了 lambda 函數)。
然后我們在save_data
函數中將其定義為 global,以便稍后其他函數或主程序可以知道它是關閉new_window
之前的最后一個條目。
在這里,我按照上面的說明修改了您的代碼,並且已經對其進行了測試,並且可以正常工作。
import tkinter as tk
def save_data(entry, t):
global last_entry
last_entry = entry.get()
t.config(text = last_entry)
def close_action(current_window):
current_window.destroy()
def insertMainInfo(last_entry):
new_window = tk.Toplevel()
new_window.geometry("307x131")
new_window.title("TestWindow")
test_entry = tk.Entry(new_window)
test_entry.place(relx = 0.283, rely = 0.1, height = 24, width = 127)
text = tk.Label(new_window, text=last_entry)
text.place(relx = 0.283, rely = 0.25, height = 24, width = 127)
save_button = tk.Button(new_window, command = lambda: save_data(test_entry, text))
save_button.place(relx=0.283, rely=0.45, height=24, width=127)
save_button.configure(text = "Save")
close = tk.Button(new_window, command = lambda: close_action(new_window))
close.place(relx=0.283, rely=0.687, height=24, width=127)
close.configure(text = "Close")
new_window.mainloop()
# --- A Simple Data Structure ---
last_entry = ''
if __name__ == '__main__':
top = tk.Tk()
top.geometry("307x131+557+330")
top.resizable(width=False, height=False)
top.title("MainWindow")
new_window_button = tk.Button(top, command = lambda: insertMainInfo(last_entry))
new_window_button.place(relx=0.283, rely=0.687, height=24, width=127)
new_window_button.configure(text = "Test")
main_label = tk.Label(top)
main_label.place(relx=0.033, rely=0.153, height=41, width=284)
main_label.configure(text = "TestLabel")
top.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.