58

I am trying to clear out a frame in the tkinter so that the new contents can be written (refresh information) but i could not manage to do it. I am aware of these

frame.destroy()
frame.pack_forget()
frame.grid_forget()

but frame.destroy() will totally remove the frame. And the other two also could not give me the result i want.What i need is just to clear up every items in the frame but the frame itself will stay. Is there anyway to do it?

Chris Aung
  • 9,152
  • 33
  • 82
  • 127

3 Answers3

97

https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/universal.html

w.winfo_children()
Returns a list of all w's children, in their stacking order from lowest (bottom) to highest (top).

for widget in frame.winfo_children():
    widget.destroy()

Will destroy all the widget in your frame. No need for a second frame.

Minion Jim
  • 1,239
  • 13
  • 30
Tom.Slick
  • 1,071
  • 1
  • 8
  • 5
38

pack_forget and grid_forget will only remove widgets from view, it doesn't destroy them. If you don't plan on re-using the widgets, your only real choice is to destroy them with the destroy method.

To do that you have two choices: destroy each one individually, or destroy the frame which will cause all of its children to be destroyed. The latter is generally the easiest and most effective.

Since you claim you don't want to destroy the container frame, create a secondary frame. Have this secondary frame be the container for all the widgets you want to delete, and then put this one frame inside the parent you do not want to destroy. Then, it's just a matter of destroying this one frame and all of the interior widgets will be destroyed along with it.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    I managed to solve my problem by creating a function that will recreate the frame i want to clear up every time i want it to update.Thanks a lot for your help Bryan, your answer was informative. – Chris Aung Apr 04 '13 at 01:16
  • 1
    @Chris Aung: Would you mind sharing that function? I am searching for just that, because my own fails for some reason. – Zelphir Kaltstahl Aug 09 '15 at 17:23
  • 3
    Here one can find a method, which works: http://stackoverflow.com/a/15995920/1829329 – Zelphir Kaltstahl Aug 10 '15 at 09:45
14

For clear frame, first need to destroy all widgets inside the frame,. it will clear frame.

import tkinter as tk
from tkinter import *
root = tk.Tk()

frame = Frame(root)
frame.pack(side="top", expand=True, fill="both")

lab = Label(frame, text="hiiii")
lab.grid(row=0, column=0, padx=10, pady=5)

def clearFrame():
    # destroy all widgets from frame
    for widget in frame.winfo_children():
       widget.destroy()
    
    # this will clear frame and frame will be empty
    # if you want to hide the empty panel then
    frame.pack_forget()

frame.but = Button(frame, text="clear frame", command=clearFrame)
frame.but.grid(row=0, column=1, padx=10, pady=5)

# then whenever you add data in frame then you can show that frame
lab2 = Label(frame, text="hiiii")
lab2.grid(row=1, column=0, padx=10, pady=5)
frame.pack()
root.mainloop()
Kickokick
  • 23
  • 5
Sagar Davara
  • 408
  • 7
  • 13