2

In one of the views there is a button for close the actual view, and it works, but when I try to open again the view it shows me the next error:

Exception in Tkinter callback
Traceback (most recent call last):
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1545, in __call__
        return self.func(*args)
      File "/home/htm97/Documents/data/workspace/repositories/projects-h/locker-system/src/gui/MainMenu.py", line 27, in verify_lockers_window
        self.app = vl.Lockers(self.vlWindow)
      File "/home/htm97/Documents/data/workspace/repositories/projects-h/locker-system/src/gui/Lockers.py", line 19, in __init__
        self.buttonsList[i].grid(columnspan = 4)
      File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2032, in grid_configure
        + self._options(cnf, kw))
TclError: bad window path name ".140687059771120.140687059776216.140687059776504.140687059776576"

The function to destroy the window is:

def close_windows(self):                                                                                                                   
    "This function destroys the window"                                                                                                    
    self.master.destroy() 

This is the view:

import Tkinter as tk
class Lockers:
    lockerList = ["1", "2", "3", "4", "5"]
    buttonsList = []

    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.master.minsize(width = 250, height = 200)
        self.initialize_lockers()

        self.frame.grid()

        tk.Label(self.frame, text = "Seleccione el locker que desea revisar:").grid(row = 0, columnspan = 4)

        i = 0
        while i < len(self.lockerList):
            self.buttonsList[i].grid(columnspan = 4) #HERE THE ERROR
            i += 1

        tk.Label(self.frame, text = "").grid(columnspan = 4)
        self.quitButton = tk.Button(self.frame, text = 'Salir', width = 8, command = self.close_windows)
        self.quitButton.grid(column = 1, columnspan = 2)

The function initialize_lockers() appends some buttons to the buttonsList.

After reading a while I've found that after executing destroy() and trying to grid something it will shows an error, but I don't understand why.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    how do you try to open again ? You destroy window so you have to create it again. – furas Nov 20 '17 at 04:59
  • 3
    `destroy` does exactly what it's name says. After destroying a widget, you can't use it again. If you want to hide a widget, use `grid_forget` instead of destroy (or `wm_iconify` on `Tk` widgets) – Daniel Nov 20 '17 at 07:05
  • "how do you try to open again ? You destroy window so you have to create it again" @furas. When I destroy it, then I press the button that creates the view, and it opens a window but without the objects inside the list. Thanks for answering – Hamilton Tobon Mosquera Nov 20 '17 at 20:11
  • "destroy does exactly what it's name says. After destroying a widget, you can't use it again. If you want to hide a widget, use grid_forget instead of destroy (or wm_iconify on Tk widgets)." @Coal_. I tried with `grid_forget()` but it only removes the widgets in the window and it does not close the window. What I want to do is to close the window and open it again without problems. Thanks for answering. – Hamilton Tobon Mosquera Nov 20 '17 at 20:14
  • 1
    where is code with `self.app = vl.Lockers(self.vlWindow)` ? What is `self.vlWindow` ? Did you recreated `self.vlWindow` when you open again ? And you don't have to cite our comment in your comment. And your don't have "thanks for answering". – furas Nov 20 '17 at 22:11
  • Yes I did, each time that I want to open that specific view I press the button that contains this function: `def verify_lockers_window(self): self.vlWindow = tk.Toplevel(self.master) self.app = vl.Lockers(self.vlWindow)` – Hamilton Tobon Mosquera Nov 21 '17 at 02:29
  • Try `self.quitButton.winfo_toplevel.wm_iconify()`. – Daniel Nov 21 '17 at 07:56

1 Answers1

2

The problem was that I had the buttonsList declared outside of the methods, as an attribute, and when I destroy the instance of the class, the attributes disappear, so I had to declare the list inside the constructor, doing this I have no problem with the destroy.

halfer
  • 19,824
  • 17
  • 99
  • 186