Python Tkinter清除帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15781802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Tkinter clearing a frame
提问by Chris Aung
I am trying to clear out a frame in the tkinter so that the new contents can be written (refresh information) but i could not manage to do it. I am aware of these
我试图清除 tkinter 中的一个框架,以便可以写入新内容(刷新信息),但我无法做到。我知道这些
frame.destroy()
frame.pack_forget()
frame.grid_forget()
but frame.destroy() will totally remove the frame. And the other two also could not give me the result i want.What i need is just to clear up every items in the frame but the frame itself will stay. Is there anyway to do it?
但 frame.destroy() 将完全删除框架。而另外两个也无法给我想要的结果。我需要的只是清除框架中的每个项目,但框架本身会保留。有没有办法做到这一点?
采纳答案by Bryan Oakley
pack_forgetand grid_forgetwill only remove widgets from view, it doesn't destroy them. If you don't plan on re-using the widgets, your only real choice is to destroy them with the destroymethod.
pack_forget并且grid_forget只会从视图中删除小部件,它不会破坏它们。如果您不打算重新使用这些小部件,那么您唯一真正的选择就是使用该destroy方法销毁它们。
To do that you have two choices: destroy each one individually, or destroy the frame which will cause all of its children to be destroyed. The latter is generally the easiest and most effective.
要做到这一点,您有两种选择:单独销毁每个框架,或者销毁将导致其所有子项都被销毁的框架。后者通常是最简单和最有效的。
Since you claim you don't want to destroy the container frame, create a secondary frame. Have this secondary frame be the container for all the widgets you want to delete, and then put this one frame inside the parent you do notwant to destroy. Then, it's just a matter of destroying this one frame and all of the interior widgets will be destroyed along with it.
由于您声称不想破坏容器框架,因此请创建一个辅助框架。有这样的次框架对所有要删除的小部件的容器,然后把你的父母这里面一帧不希望破坏。然后,只需销毁这一框架,所有内部小部件都将随之销毁。
回答by Tom.Slick
https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/universal.html
https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/universal.html
w.winfo_children()
Returns a list of all w's children, in their stacking order from lowest (bottom) to highest (top).
w.winfo_children()
按从最低(底部)到最高(顶部)的堆叠顺序返回所有 w 子项的列表。
for widget in frame.winfo_children():
widget.destroy()
Will destroy all the widget in your frame. No need for a second frame.
将销毁框架中的所有小部件。不需要第二帧。
回答by Sagar Davara
For clear frame, first need to destroy all widgets inside the frame,. it will clear frame.
要清除框架,首先需要销毁框架内的所有小部件。它将清除框架。
import tkinter as tk
from tkinter import *
root = tk.Tk()
frame = Frame(root)
frame.pack(side="top", expand=True, fill="both")
lab = Label(frame, text="hiiii")
lab.grid(row=0, column=0, padx=10, pady=5)
frame.but = Button(frame, text="clear frame", command=clearFrame)
frame.but.grid(row=0, column=1, padx=10, pady=5)
def clearFrame():
# destroy all widgets from frame
for widget in frame.winfo_children():
widget.destroy()
# this will clear frame and frame will be empty
# if you want to hide the empty panel then
frame.pack_forget()
# then whenever you add data in frame then you can show that frame
lab2 = Label(frame, text="hiiii")
lab2.grid(row=1, column=0, padx=10, pady=5)
frame.pack()

