Destroying a Toplevel tk window in python

I was trying to write code that would automatically close the Toplevel Tk window in Python.

I ended up getting it working, but ran into a small problem along the way that I was unable to figure out.

Two two buttons work, but the first one doesn't work 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()

      

+1


source to share


1 answer


Because it returns a function, not its result.

You must put:

command=TL.destroy

      



or if you used a lambda:

command=lambda: TL.destroy()

      

+8


source







All Articles