24
import tkinter


class App():
   def __init__(self):
       self.root = Tkinter.Tk()
       button = Tkinter.Button(self.root, text = 'root quit', command=self.quit)
       button.pack()
       self.root.mainloop()

   def quit(self):
       self.root.destroy 

app = App()

How can I make my quit function to close the window?

nbro
  • 15,395
  • 32
  • 113
  • 196
DRdr
  • 1,171
  • 3
  • 12
  • 12

4 Answers4

72
def quit(self):
    self.root.destroy()

Add parentheses after destroy to call the method.

When you use command=self.root.destroy you pass the method to Tkinter.Button without the parentheses because you want Tkinter.Button to store the method for future calling, not to call it immediately when the button is created.

But when you define the quit method, you need to call self.root.destroy() in the body of the method because by then the method has been called.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
4
class App():
    def __init__(self):
        self.root = Tkinter.Tk()
        button = Tkinter.Button(self.root, text = 'root quit', command=self.quit)
        button.pack()
        self.root.mainloop()

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

app = App()
theB
  • 6,450
  • 1
  • 28
  • 38
3
def exit(self):
    self.frame.destroy()
exit_btn=Button(self.frame,text='Exit',command=self.exit,activebackground='grey',activeforeground='#AB78F1',bg='#58F0AB',highlightcolor='red',padx='10px',pady='3px')
exit_btn.place(relx=0.45,rely=0.35)

This worked for me to destroy my Tkinter frame on clicking the exit button.

saqib1707
  • 51
  • 1
  • 4
0
class App():
    def __init__(self):
        self.root = Tkinter.Tk()
        button = Tkinter.Button(self.root, text = 'root quit', command=self.quit)
        button.pack()
        self.root.mainloop()

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

app = App()
buddemat
  • 4,552
  • 14
  • 29
  • 49
  • 1
    Welcome to StackOverflow. While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – Ruli Nov 23 '20 at 09:21