Question: I've seen some usage of `self.destroy()` within classes but I couldn't get it working with what I wanted it to do.

I have the class `resultsPage` that shows results obtained on another page. I have made the `displayResults(pageNo)` function to show these when `resultsPage` is visible. The problem arises with the back and next buttons which are made to go between pages of results. All widgets are created on top of each other but I want to remove them all then create the new ones. I added `self.destroy()` to try and fix this but it didn't work.

I'm not sure if it's to do with the placement of where I'm defining my functions but I have had a play around with where they're defined and it hasn't changed the error message.

This is a simplified example of my code:

```
class resultsPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

    def onShowFrame(self, event):
        def displayResults(pageNo):
            self.destroy()
            #Create widgets related to pageNo
            #Create back and next buttons e.g.
            back = tk.Button(self, text="<=",
                                 command=lambda: displayResults(pageNo - 1))
        displayResults(1)

```

The error I get is: `_tkinter.TclError: bad window path name ".!frame.!previewresultspage"`

If it helps, I can post my full code but I thought I'd generalise it so it's more helpful to others.

Answer: 
The problem comes from the line:

```
tempArray = finalValues[-1]

```

You don't create a copy of the previous list, but only a new name to refer to it. After that, all changes you make to `tempArray` are actually changes to this list, and when you finally do:

```
finalValues.append(tempArray)

```

you just add another reference to this same list in `finalValues`.

In the end, `finalValues` contains 4 references to the same list, which you can access with `finalValues[0]`, `finalValues[1]`...

What you need is to create a new list by copying the previous one. One way to do it is to use a slice:

```
tempArray = finalValues[-1][:]

```

You can find other ways to close or copy a list in [this question](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list)

And so, the complete code gives the expected output:

```
Last array of final:  [1, 2, 3, 4]
Temp Array:  [1, 2, 3, 4]
Final Values:  [[1, 2, 3, 4], [2, 3, 4, 1]]
Last array of final:  [2, 3, 4, 1]
Temp Array:  [2, 3, 4, 1]
Final Values:  [[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2]]
Last array of final:  [3, 4, 1, 2]
Temp Array:  [3, 4, 1, 2]
Final Values:  [[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]]

[[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]]

```
The problems is in `shallow` assignment of arrays. You should make `deep` copy, to really clone arrays, to make them independent.

I did it in your own code. There are a few changes of your code: 

1. `import copy` that it have been added to first row.
2. Three usages of `copy.deepcopy` function instead of `=`(simple assignment).

```py

    import copy

    def numbers(list_of_numbers):
        finalValues = copy.deepcopy([list_of_numbers])
        #print(list_of_numbers)
        for i in range(1,len(list_of_numbers)):
            print("Last array of final: ", finalValues[-1])
            tempArray = copy.deepcopy(finalValues[-1])
            print("Temp Array: ",tempArray)
            temp = tempArray[0]
            for j in range(0,len(list_of_numbers)-1):
                tempArray[j] = tempArray[j+1]
            tempArray[-1] = temp
            finalValues.append(copy.deepcopy(tempArray))

            print("Final Values: ",finalValues)
        return finalValues

    numbers([1,2,3,4])

```

> 
> Program Output
> 
> 
> 

```
[[4, 1, 2, 3], [4, 1, 2, 3], [4, 1, 2, 3], [4, 1, 2, 3]]

```

> 
> Program Output
> 
> 
> 

```
[[1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]]

```