1

I'm having an issue with my Python code, I'm using Tkinter to make an App and switch between windows, one of the windows has the following code:

def __init__(self, master):
    tk.Frame.__init__(self, master)
    master.title("Chain Targets:")
    master.geometry("400x800")
    tk.Label(self, text="Chain Targets").grid()
    self.sbox = tk.Listbox(master)
    self.sbox.grid(row=0, column=0)
    number=1
    for k in sorted(target_list, key=target_list.get, reverse=True):
        place_target= str(number) + "   " + k + "    " + str(target_list[k])
        self.sbox.insert(number,place_target)
        number+=1
    tk.Scrollbar.grid(self)

    tk.Button(self, text="Main Menu",
              command=lambda: master.switch_frame(MainPage)).grid()

Now this all works fine, except when using the switch_frame function the Listbox stays in the new screen, the switch_frame function is the following:

    def switch_frame(self, frame_class):
    """Destroys current frame and replaces it with a new one."""
    new_frame = frame_class(self)
    if self._frame is not None:
        self._frame.destroy()
    self._frame = new_frame
    self._frame.grid()

somehow I can't wrap my head around the fact that the self._frame.destroy() doesn't destroys the Listbox and it stays in every window in the top, for the rest my code does everything it's intended to do. I hope someone has an idea of what I am missing.

2
  • 1
    Because its parent is master, i.e. the root window. Commented May 7, 2020 at 10:40
  • 1
    Ok, thanks, I see now. That helps a lot! Commented May 7, 2020 at 10:45

1 Answer 1

1

The problem is that you set the master as listbox parent. Use self.sbox = tk.Listbox(self) instead. Hope that's helpful!

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I was already pointed to that, now it seems stupid to not have noticed this.... Guess I was blocked. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.