How do I execute a command after pushing a button while also closing the window with the button in python (with tkinter)?

时间:2016-07-11 21:10:07

标签: python python-3.x tkinter

Basically I want to execute the command as seen below and also close the window with the 2 buttons (in either case). The command just sets the variable self.switch to True or False, so if there is a neater way of doing this rather than writing a new method for each of those, that would also be nice.

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()

1 个答案:

答案 0 :(得分:0)

Doing it in a new method is the neat way of doing things- you'd probably make the command=lambda:self.switchTo([value]), with [value] being True or False that you would like to swap to. You would have to pass top into the function as well, so you can destroy it.

Doing this destruction with Tk() is (normally) bad practice, as Tk() creates a Tcl/Tk interpreter- you really just want to instantiate it once and hide it typically.