简体   繁体   English

Python Tkinter清除帧

[英]Python Tkinter clearing a frame

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.我试图清除 tkinter 中的一个框架,以便可以写入新内容(刷新信息),但我无法做到。 I am aware of these我知道这些

frame.destroy()
frame.pack_forget()
frame.grid_forget()

but frame.destroy() will totally remove the frame.但 frame.destroy() 将完全删除框架。 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?有没有办法做到这一点?

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() w.wininfo_children()
Returns a list of all w's children, in their stacking order from lowest (bottom) to highest (top).返回所有 w 的孩子的列表,按照从最低(底部)到最高(顶部)的堆叠顺序。

for widget in frame.winfo_children():
    widget.destroy()

Will destroy all the widget in your frame.将销毁框架中的所有小部件。 No need for a second frame.不需要第二帧。

pack_forget and grid_forget will only remove widgets from view, it doesn't destroy them. pack_forgetgrid_forget只会从视图中删除小部件,它不会破坏它们。 If you don't plan on re-using the widgets, your only real choice is to destroy them with the destroy method.如果您不打算重新使用这些小部件,那么您唯一真正的选择是使用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 not want 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.然后,只需销毁这一框架,所有内部小部件都将随之销毁。

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)

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()

frame.but = Button(frame, text="clear frame", command=clearFrame)
frame.but.grid(row=0, column=1, padx=10, pady=5)

# 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()
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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