I am trying to use the withdraw method, but it’s not working. Here is my code:
from tkinter import *
class GameBoard(Frame):
def __init__(self):
top = Toplevel()
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._hide)
self.__buttonA1.grid()
def _hide(self):
top.withdraw()
def main():
GameBoard().mainloop()
main()
How would I make my command so that it hides the toplevel created window?
I want to be able to hide it until needed by the main program.
When you run the code you get an error message. What does that error message tell you? It should have the clues to understand what the problem is.
The error is “NameError: global name ‘top’ is not defined”. So the question you should ask yourself is “why does the program think “top” is global, and why is it not defined?”.
Looking at your code I can see that you’re using a local variable named “top” to store the reference to the window. You need to either declare that as global or declare it as an attribute on your class. The latter is the proper way.
To do this, simply change
toptoself.topeverywhere in your code.