How do I make the destroy () method in tkinter work with my code?

from tkinter import * class GameBoard(Frame): def __init__(self): Frame.__init__(self) self.master.title("test") self.grid() #button frame self.__buttonPane = Frame(self) self.__buttonPane.grid() #buttons self.__buttonA1 = Button(self.__buttonPane,text = "A1",command = self._close) self.__buttonA1.grid() def _close(self): GameBoard().destroy() def main(): GameBoard().mainloop() main() 

How can I make my function close to work?

+4
source share
2 answers
 GameBoard() 

creates a new instance of GameBoard . Therefore:

 GameBoard().destroy() 

creates a new instance and calls destroy() on it, which does not affect the existing instance.

You want to access the current instance in your _close() method, which is done via self :

 def _close(self): self.destroy() 

However, this only destroys the frame (and its child windows, like a button), and not the top-level window (master).

To completely close the user interface, you can call self.master.destroy() or just self.quit() :

 def _close(self): self.quit() 
+7
source

self.master.destroy () will close both the parent and the child process (see, for example, IE). self.destroy will close the child process. I know this is an old post, but I decided that this information could be applicable to someone.

those.

 def _close(self): self.master.destroy() 
0
source

All Articles