Take the 2-minute tour ×

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()
    #button frame
    self.__buttonPane = Frame(self)
    self.__buttonA1 = Button(self.__buttonPane,text = "A1",command = self._hide)

  def _hide(self):

def 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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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 top to self.top everywhere in your code.

self.top = Toplevel()
share|improve this answer

Your Answer


