[英]Closing a Toplevel Tkinter window
我試圖自學Python,對可能是一個愚蠢的問題表示歉意,但這已經讓我發瘋了幾天。 我在這里查看了關於同一主題的其他問題,但似乎仍然無法解決這個問題。
我創建了一個頂層窗口,要求用戶提示,並希望在用戶按下他們選擇的按鈕時關閉該窗口。 這就是問題所在,我無法為了愛情或金錢而關閉它。 我的代碼包含在下面。
非常感謝您的幫助。
from Tkinter import *
root = Tk()
board = Frame(root)
board.pack()
square = "Chat"
cost = 2000
class buyPrompt:
def __init__(self):
pop = Toplevel()
pop.title("Purchase Square")
Msg = Message(pop, text = "Would you like to purchase %s for %d" % (square, cost))
Msg.pack()
self.yes = Button(pop, text = "Yes", command = self.yesButton)
self.yes.pack(side = LEFT)
self.no = Button(pop, text = "No", command = self.noButton)
self.no.pack(side = RIGHT)
pop.mainloop()
def yesButton(self):
return True
pop.destroy
def noButton(self):
return False
我嘗試了很多不同的pop.destroy
方法,但是似乎都沒有用,我嘗試過的方法是;
pop.destroy()
pop.destroy
pop.exit()
pop.exit
謝謝
調用的方法確實是pop
對象上的destroy
。
但是,在yesButton
方法內部, pop
引用了未知的內容。
在初始化對象時,應在__init__
方法中將pop
項作為self
的屬性:
self.pop = Toplevel()
然后,在yesButton
方法內部,對self.pop
對象調用destroy
方法:
self.pop.destroy()
關於pop.destroy
和pop.destroy()
之間的區別:
在Python中,幾乎所有東西都是對象。 因此,方法也是一個對象。
編寫pop.destroy
,您引用的方法對象名為destroy
,並且屬於該pop
對象。 . 它基本上與編寫1
或"hello"
:它不是語句,或者,如果您願意,也不是 。
the pop.destroy
object, that is, to execute its __call__
method. 編寫pop.destroy()
,您告訴Python pop.destroy
對象,即執行其__call__
方法。
換句話說,編寫pop.destroy
不會做任何事情(除了在交互式解釋器中運行時,打印類似<bound method Toplevel.destroy of...>
類的東西),而pop.destroy()
將有效地運行pop.destroy
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.