[英]How do I make a window immediately close after pressing the button that's inside that window
很難用文字來解釋,所以我將通過順序圖片來說明 go
這是它最初的樣子:圖 1
這是我的代碼:
`import tkinter as tk
import tkinter.messagebox
class Test():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("500x400")
self.b1 = tk.Button(self.root, width=11, text="btn1", font=('Satoshi', 15),command=self.add).pack(padx=10, pady=10)
self.root.mainloop()
def add(self):
extra = tk.Toplevel(self.root)
extra.geometry("500x200")
button = tk.Button(extra, text="btn1", font=('Satoshi', 15), command=self.added).pack()
def added(self):
tkinter.messagebox.showinfo("test", "button pressed succesfully!")
`
看起來不錯,那么我將按下第二個按鈕:
我希望它看起來像這樣:
所以在我按下按鈕 2 window 之后,它會關閉並且會出現消息框,提示我已成功按下按鈕。
對不起,我真的不能用語言來解釋它。 但這就是我的問題的要點。 我想這樣做,以便當我按下按鈕 2 時,它的 window 將關閉並出現消息框。
tkinter還是新人。PS:我不想關閉整個程序。 我確實知道如何使用 destroy 命令,但事實並非如此。
您需要根據您的 msgbox 響應在額外的 window 中使用 destroy() ..
import tkinter as tk
import tkinter.messagebox
class Test(tk.Tk):
def __init__(self):
self.root = tk.Tk()
self.root.geometry("500x400")
self.b1 = tk.Button(self.root, width=11, text="btn1", font=('Satoshi', 15),command=self.add).pack(padx=10, pady=10)
self.root.mainloop()
def add(self):
self.extra = tk.Toplevel(self.root)
self.extra.geometry("500x200")
button = tk.Button(self.extra, text="btn1", font=('Satoshi', 15), command=self.added).pack()
def added(self):
msg_result = tkinter.messagebox.showinfo("test", "button pressed succesfully!")
if msg_result == 'ok':
self.extra.destroy()
app = Test()
app.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.