0

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)

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Hi yes, I have used it but it only destroys one frame. I have no single frame I have multiple frames in tabs. That is if I destroy tabs then the GUI will be of no use.
Do you want to destroy the tabs, or the contents of the tabs?
I want to delete contents in tabs because if I delete tabs only the GUI with a big box is visible. I cannot import images if I delete the tabs. The format I am using is I have three tabs and in those three tabs, I have three frames. Now I want to delete the contents in all three frames. These three frames are not inside each other but they are individual like each frame in each tab. for ex: tab1 contains frame 1 , tab 2 contains frame 2 like that. I hope this clears the confusion
0

I faced this problem How to delete multiple frames in tkinter and today I approach to this solution. Hope it could help in further other python learners. Here is my code:

import os
from tkinter import *
from tkinter import messagebox

os.system('cls||clear')
window = Tk()
window.title("Menu Bar")

frames = []

def delete_frames():

    Frame.destroy(frames.pop()) 
    if frames == []:
        messagebox.showinfo("Warning","All frames were Deleted")

def new_text():

    frame = Frame(window, width=300, height=180)
    frame.pack_propagate(False)  
    frame.pack()

    txt = Text(frame,wrap='word')
    txt.pack(expand=True, fill='both') 

    frames.append(frame)
    print(frames)

    v_scroll_bar = Scrollbar(txt,orient=VERTICAL,command=txt.yview) 
    v_scroll_bar.pack(side = RIGHT, fill="y")
    txt.config(yscrollcommand=v_scroll_bar.set)

txt_btn = Button(window,text="Create Frames",command=new_text)
txt_btn.pack()

del_frames = Button(window,text="Delete Frames", command= delete_frames)
del_frames.pack(padx=220)

window.geometry("750x850")
window.mainloop()

Comments

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.