title: Python 3.x _tkinter.TclError: bad window path name ".!toplevel"
body: <p>I was looking to do a popup widget in tkinter where the user can input some data. But while I was testing I came up with the following error:</p>
  
  <pre><code>  File "returnWeeksGUI_v1.py", line 363, in __init__
      self.content = program.popup("Add_label")
    File "returnWeeksGUI_v1.py", line 296, in popup
      self.w=popupWindow(self.master,popup_type)
    File "returnWeeksGUI_v1.py", line 42, in __init__
      self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
    File "C:\Users\Fernanda\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2369, in __init__
      Widget.__init__(self, master, 'button', cnf, kw)
    File "C:\Users\Fernanda\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2299, in __init__
      (widgetName, self._w) + extra + self._options(cnf))
  _tkinter.TclError: bad window path name ".!toplevel"
  </code></pre>
  
  <p>It has been 3 days of debugging and editing the code, but still hadn't found the appropiate solution. </p>
  
  <hr>
  
  <p><strong>The code</strong></p>
  
  <p>I have some imports and variables:</p>
  
  <pre><code>import tkinter as tk
  
  models = {
      "text":["TXT",50,50],
      "DD":["DD",50,50],
      "MM":["MM",50,50],
      "AA":["AA",50,50],
      "MC":["MC",50,50],
      "DP":["DP",50,50],  
      }
  </code></pre>
  
  <p>I have a tkinter class:</p>
  
  <pre><code>class App(tk.Frame):
      def __init__(self,master=None):
          super().__init__(master)
          self.master = master
  
          #This class has some variables:
          self.pu= program.popup("Add_label")
  
      def popup(self,popup_type):
              self.w=popupWindow(self.master,popup_type)
              self.master.wait_window(self.w.top)
  </code></pre>
  
  <p>I also have a popup code gotten from <a href="https://stackoverflow.com/questions/10020885/creating-a-popup-message-box-with-an-entry-field">a question in StackOverflow</a>, which I modified for my purposes. It shows up some radiobuttons/entries that the user has to complete/select.</p>
  
  <pre><code>class popupWindow(object):
      def __init__(self,master,win_type):
          top=self.top=tk.Toplevel(master)
          if str(win_type) == "Add_label":
              """
              Here goes a large piece of code that is irrelevant.  
              """
              # Until I have this set of statements which break the program.
              self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
              self.bu.pack()
  
          elif str(win_type) == "Add_box":
              #...
              self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
              self.bu.pack()
  
          elif str(win_type) == "Delete_label":
              #...
              self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
              self.bu.pack()
  
          elif str(win_type) == "Delete_box": 
              #...
              self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
              self.bu.pack()
  
      def cleanup(self,win_type):
          #...
          # At the end, a destroy method is called.
          self.top.destroy()
  </code></pre>
  
  <p>The instance is like this:</p>
  
  <pre><code>root = tk.Tk()
  program = App(root)
  program.mainloop()
  </code></pre>
  
  <p><em>The popup is not triggered by a variable in the <code>__init__()</code> function. In fact it shows up when a certain button is pressed. However, I replaced it here with a variable instead, because it would be easier to analyze. The button that triggers the popup is fine. I've checked it before.</em></p>
  
  <hr>
  
  <p>I would be grateful if someone could help. Thanks.</p>