Why I am unable to close the A Tkinter frame and create another one?
Source: Dev.to
Question
I am making a wizard where it generates a frame while closing another one upon clicking Next using Tkinter. My issue is that the previous frame is not properly closed, as shown in the screenshot (not included here).
Code
import os
import tkinter as tk
from tkinter import ttk
class SelectBodyScreen(ttk.Frame):
def __init__(self, root, lang, prod_text, test_text, on_next):
super().__init__(root)
self.lang = lang
self.prod_text_content = prod_text
self.test_text_content = test_text
self.root = root
self.build_ui()
self.on_next = on_next
def next_clicked(self):
selected_body = self.final_text.get("1.0", tk.END).strip()
self.on_next(selected_body)
def use_prod(self):
text =
# …
Issue
When the Next button is pressed, the new frame appears, but the previous frame remains visible (or only partially disappears). The expected behavior is for the old frame to be completely destroyed before the new one is displayed.
Possible causes to investigate
- The old frame may not be explicitly destroyed (
frame.destroy()). - The geometry manager (e.g.,
pack,grid,place) might still be managing the old widget. - References to the old frame could be retained, preventing garbage collection.
Consider adding a method that calls self.destroy() (or self.pack_forget() / self.grid_forget()) on the current frame before creating and packing the next one.
Note: The original post included a screenshot illustrating the improper closure; it has been omitted here.