2

I am new to tkinter and I was wondering if there is a way to clear whole root (window). I tried with root.destroy() , but I want to have a chance to undo(to callback some widgets). I also tried .pack_forget() and .grid_forget() , but it takes a lot of time and later may cause many bugs in program.

Thank you for help.

Luka1
  • 71
  • 2
  • 9
  • Have you tried .grid_remove()? Also, see [this answer](http://stackoverflow.com/questions/3819354/in-tkinter-is-there-any-way-to-make-a-widget-not-visible/5928294#5928294) – Aleksander Monk Feb 26 '15 at 21:35

2 Answers2

2

If you plan on literally destroying everything in the root window, my recommendation is to make a single frame be a child of the root window, and then put everything inside that frame. Then, when you need to clear the root window you only have to delete this one widget, and it will take care of deleting all of the other widgets.

If instead of destroying the widgets you want to merely hide them, the best solution is to use grid, so that you can use grid_forget to remove them from view, and grid to make them visible again. You can use pack and pack_forget, but pack doesn't remember where the widget was, making it more difficult to restore them without a lot of work.

If your program is made of logical groups of widgets, use frames to create the groups, and then you can destroy or call grid_forget on the entire frame at once, rather than having to do that for each individual widget.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
2
for ele in root.winfo_children():
  ele.destroy()
Kaizhen Ding
  • 33
  • 1
  • 5
  • 1
    Helpful if explain you answer instead of code only. – αƞjiβ Aug 13 '15 at 17:43
  • So root is the master object. root.winfo_children() lists all the child weights in the root, and by giving the command .destroy to every weight, you basically clean up the entire root – Kaizhen Ding Aug 13 '15 at 23:52