I am building an image processing methods GUI in that I have created some frames to show various methods in each frame as a tab using the notebook functionality of the Tkinter. How can I create a function that deletes all four frames, so that when I import the second image the first one will be removed and the newly imported image is added to the frames?
I have tried the winfo_children function to destroy frames but it only works for one frame at a time. I also tried looping through all frames. my skills are not sufficient to think through this one. So, I need a suggestion or anything that I m missing.
Thank you so much for your time any help is appreciated.
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tab4 = ttk.Frame(tabControl)
tabControl.add(tab1, text='Imported Image')
tabControl.add(tab2, text='Method 1')
tabControl.add(tab3, text='Method 2')
tabControl.add(tab4, text='Method 3')
tabControl.pack(expand=1, fill="both")
frame1_title= tk.Label(tab1,bd=10)
frame1_title.pack(fill='both', expand=True)
frame2_title= tk.Label(tab2, font='times 35')
frame2_title.pack(fill='both', expand=True)
frame3_title= tk.Label(tab3,font='times 35')
frame3_title.pack(fill='both', expand=True)
You can use winfo_children
to do this.
for tab in tabControl.winfo_children():
for child in tab.winfo_children():
child.destroy()
winfo_children
returns a list of child widgets. The outer loop gets all the tab frames, then the inner loop destroys the tab contents of each frame.
Edit
To not delete the canvas, you can use the isinstance
function. This will check if the widget is a canvas and will only destroy it if it is not.
for tab in tabControl.winfo_children():
for child in tab.winfo_children():
if not isinstance(child, tk.Canvas):
child.destroy()