Close a tkinter window?

Tags: tkinter python
Question!

How do I end a Tkinter program? Let's say I have this code:

from Tkinter import *

def quit():
    # code to exit

root = Tk()
Button(root, text="Quit", command=quit).pack()
root.mainloop()

How should I define the quit function?



Answers

Try this:

from Tkinter import *
root = Tk()
Button(root, text="Quit", command=lambda:quit).pack()
root.mainloop()


Illumination in case of confusion...

def quit(self):
    self.destroy()
    exit()

A) destroy() stops the mainloop and kills the window, but leaves python running

B) exit() stops the whole process

Just to clarify in case someone missed what destroy() was doing, and the OP also asked how to "end" a tkinter program.



Use root.destroy()! Works for me just fine.



This video can help you solving your question :)
By: admin