I was trying to write code that would auto-close a Toplevel Tk window in Python.
I ended up getting it to work, but ran into a little problem along the way that I wasn’t able to figure out.
The second two buttons work, but the first one doesn’t and I don’t understand why…
Any ideas?
from Tkinter import * root = Tk() def doDestroy (): TL.destroy() TL = Toplevel() TL.b = Button (TL, text='lambda destroy', command=lambda: TL.destroy) TL.b.pack() TL.b2 = Button (TL, text='callback destroy', command=doDestroy) TL.b2.pack() de = lambda: TL.destroy() TL.b3 = Button (TL, text='lambda that works', command=de) TL.b3.pack() root.mainloop()
Because it returns a function and not its result.
You should put:
or if you used lambda: