简体   繁体   中英

How to delete all the widgets but keep the window in python tkinter?

I want to display another GUI frame after my first one, in the same window. How can I delete the entries, labels and buttons only?

I've tried root.destory and pack.forget but I have not managed to get it right.

If this is something you expect to do a lot, one very simple solution is to put a frame as the only widget directly in the root window. Then, put all of your other widgets in the frame. When you are ready to delete all the widgets, all you have to do is destroy that frame and all of its children will automatically be destroyed.

Though, frankly, it's trivial just to iterate over the result of root.winfo_children() and delete all of the widgets.

I think this will work:

def all_children(window):
    _list = window.winfo_children()
    for item in _list:
        if item.winfo_children():
            _list.extend(item.winfo_children())
    return _list


widget_list = all_children(root)
for item in widget_list:
    item.pack_forget()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM