0

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.

Holly
  • 65
  • 10
  • Read [Switch between frames:`create=>use=>destroy()`](https://stackoverflow.com/a/49325719/7414759) – stovfl Dec 21 '19 at 19:56
  • @stovfl I had a read it but I couldn't get it working. I don't have anything called `master` to begin with and they haven't got it within `onShowFrame` which is what complicates things. I thought I might be able to use `parent` instead of `master` but that didn't work either. – Holly Dec 21 '19 at 20:13
  • Did you want such a **Switch between Frames** at all? – stovfl Dec 21 '19 at 20:21
  • I have `showFrame` which allows me to switch between frames. However, I am actually having an issue with this when creating a home button within `onShowFrame` as the console says `NameError: name 'controller' is not defined`. So yes, I would like to be able to switch between frames and I also want to clear the `resultsPage` of all widgets upon calling `displayResults`. – Holly Dec 21 '19 at 20:23

2 Answers2

0

You are deleting the widget in onShowFrame, and then immediately try to create a new widget with it as the parent. You can't use a deleted widget as the parent of another widget.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

As pointed out, using self.destroy() in this case, will not work. To achieve the goal of deleting all widgets on the frame, you can use a loop (credit to @stovfl):

for widget in self.grid_slaves():
    widget.destroy()
Holly
  • 65
  • 10