Question

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()
Was it helpful?

Solution

Because it returns a function and not its result.

You should put:

command=TL.destroy

or if you used lambda:

command=lambda: TL.destroy()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top