繁体   English   中英

如何在按下按钮后执行命令,同时在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解释器-您实际上只想实例化一次并通常hidehide

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM