![](/img/trans.png)
[英]How do i clear a window after pressing a button in Python Tkinter?
[英]How do I execute a command after pushing a button while also closing the window with the button in python (with tkinter)?
基本上,我想執行如下所示的命令,並同時使用2個按鈕關閉窗口(無論哪種情況)。 該命令只是將變量self.switch設置為True或False,因此,如果有一種更巧妙的方法來執行此操作,而不是為每個方法編寫一個新方法,那也很好。
def switchButton(self):
top = Tk()
self.a = Button(top,text="Switch", command=self.switchTrue())
self.a.pack(side=LEFT)
self.b = Button(top,text="Don't switch", command=self.switchFalse())
self.b.pack(side=RIGHT)
top.mainloop()
用一種新的方法進行操作是一種整潔的方法-您可能會想將command=lambda:self.switchTo([value])
為[value]為True或False,並希望將其交換。 您還必須將top
傳遞給該函數,以便可以destroy
它。
使用Tk()
destruction
是(通常)不好的做法,因為Tk()
創建了一個Tcl / Tk解釋器-您實際上只想實例化一次並通常hide
其hide
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.