Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to close the window in Tkinter

Tags:

python

tkinter

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?

like image 221
DRdr Avatar asked Sep 11 '25 14:09

DRdr


1 Answers

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.

like image 52
unutbu Avatar answered Sep 14 '25 03:09

unutbu