[英]Can't close a window and keep other open
我正在嘗試打開第二個 window 然后運行一些代碼並在按鈕事件上關閉第二個 window。 我已經嘗試了所有可以找到的示例,但仍然出現屬性錯誤。 任何幫助表示贊賞。 我對此很陌生,幾乎 55 歲開始了新的愛好。
我刪除了大部分代碼,以便我希望得到足夠的發布,以便有人可以幫助我。 大家聖誕快樂
# ADD NEW PASSWORD
def add_pass():
add_pass = Toplevel()
add_pass.title("Enter New Password")
add_pass.geometry('500x700')
# add_pass.resizable(0, 0)
Add_Button = Button(add_pass, text="Enter", font=("normal", 14),
command=add_butt)
Add_Button.grid(row=12, column=2, pady=30)
def add_butt():
print(Person_Entry.get())
# Create a database or connect to one
conn = sqlite3.connect('Pass.db')
c = conn.cursor()
# WRITE TEXT BOXES TO SQLITE3 DB USING VARIABLES.
PassData = [(Seq_Entry.get(), Person_Entry.get(), Name_Entry.get(),
URL_Entry.get(), Username_Entry.get(), Password_Entry.get(), Hint1_Entry.get(),
Hint2_Entry.get(), Hint3_Entry.get(), Notes_Entry.get())]
for element in PassData:
c.execute("INSERT INTO Passwords VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
element)
# Commit Changes
conn.commit()
# Close Connection
conn.close()
add_pass.destroy()
root = Tk()
root.geometry('500x500')
root.title('Password Saver')
my_menu = Menu(root)
root.mainloop()
You got several options here to achieve this, the easiest way to go would be to use lambda and pass a reference of your window, stored with the variable add_pass
in the namespace of the function add_pass
through the interface of your add_butt
. 通過 tkinter 中的按鈕命令傳遞參數可以通過不同的方式實現,但我更喜歡 lambda。
更改將如下所示:
def add_pass():
..
Add_Button = Button( ..,command=lambda window=add_pass: add_butt(window))
def add_butt(window):
window.destroy()
...
補充建議:
不要使用通配符導入
不要多次使用相同的變量名
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.