Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I make 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 would I make my function for close to work?

like image 700
Victor Oza Avatar asked Nov 30 '12 18:11

Victor Oza


People also ask

What does destroy () do in tkinter?

destroy() method in Tkinter | PythonThis method can be used with after() method. As you may observe, in above code that the command that is passed in button-2 is to destroy button-1 so as soon as you press button-2, button-2 will get destroyed.

What is the correct syntax for Destroy in tkinter?

Which is the correct syntax of destroying in tkinter? tkinter. Button(). destroy() The code create the text() through button and then destroy and created new buttom compose.

Can you destroy a frame tkinter?

If we want to clear the frame content or delete all the widgets inside the frame, we can use the destroy() method. This method can be invoked by targeting the children of the frame using winfo_children().

What does the destroy function do?

The destroy() method in Tkinter destroys a widget. It is useful in controlling the behavior of various widgets which depend on each other. Also when a process is complete by some user action we need to destroy the GUI components to free the memory as well as clear the screen. The destroy() method achieves all this.


1 Answers

GameBoard()

creates a new instance of GameBoard. Therefore:

GameBoard().destroy()

creates a new instance and calls destroy() on it which has no effect on the existing instance.

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

def _close(self):
    self.destroy()

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

To completely close the UI, you could call self.master.destroy() or simply self.quit():

def _close(self):
    self.quit()
like image 152
Anonymous Coward Avatar answered Sep 23 '22 00:09

Anonymous Coward