1,105,290 Community Members

clear and replace all widgets on tkinter frame

Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

Does anybody know how to clear all widgets; labels, buttons, etc. from a tkinter frame so that new ones may be put in their place? I've tried self.destroy() but that makes the frame unreachable, and self.grid_forget() followed by, "and without" self.grid() neither of these work, at least not as expected. any suggestions?

Member Avatar
nezachem
Practically a Posting Shark
896 posts since Dec 2009
Reputation Points: 616 [?]
Q&As Helped to Solve: 197 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes, to remove a widget you'd do widget.grid_forget(). May we see the failing code (specifically, what is self in that context)?

Member Avatar
woooee
Posting Maven
2,792 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
1
 

You can pack them all into a Frame and destroy the Frame.

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

class DestroyTest():
    def __init__(self, top):
        self.top=top
        self.top.geometry("+10+10")

        self.frame=tk.Frame(self.top)
        self.frame.grid()

        test_label=tk.Label(self.frame, text="Label")
        test_label.grid(row=1, column=0)

        destroy_button=tk.Button(self.frame, text="Destroy Frame", \
                                 command=self.destroy)
        destroy_button.grid(row=10, column=0)

        exit_button=tk.Button(self.top, text="Exit", command=top.quit)
        exit_button.grid(row=10, column=0)

    def destroy(self):
        self.frame.destroy()
        self.new_toplevel=tk.Toplevel(self.top, takefocus=True)
        self.new_toplevel.geometry("+50+50")
        self.new_toplevel.grid()

        lbl=tk.Label(self.new_toplevel, text="New Toplevel")
        lbl.grid()


root=tk.Tk()
DT=DestroyTest(root)
root.mainloop()
Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

the context was a class inherited from the tkinter Frame class being used as a Toplevel, the problem I realized was that when I chose to destroy the frame,thinking of it as the window, really I was destroying the complete contents of that window. There were so many widgets in the frame that I didn't want to deal with grid_forget-ing all of them. but I did as woooee suggested "I don't know why I didn't think of it" and added a frame within it, destroyed it, and recreated it. Below is the tentative result, I haven't "prettied it up" yet with the geometry, button height, or grid locations. Note that it is the same window, I just took the screenshot twice to show the change when next is pressed.

Edit, apparently I can't change the button height for ttk style buttons...anybody done anything to get around that?

Attachments VI.jpg 21.59KB
Question Answered as of 2 Years Ago by woooee and nezachem
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: