Why I am unable to close the A Tkinter frame and create another one?

Published: (March 4, 2026 at 12:20 PM EST)
2 min read
Source: Dev.to

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.

0 views
Back to Blog

Related posts

Read more »

No right to relicense this project

Hi, I'm Mark Pilgrim. You may remember me from such classics as Dive Into Python and Universal Character Encoding Detector. I am the original author of chardet....

Relicensing with AI-Assisted Rewrite

Disclaimer I am not a lawyer, nor am I an expert in copyright law or software licensing. The following post is a breakdown of recent community events and legal...