Python Tkinter清除一个框架[英] Python Tkinter clearing a frame

本文是小编为大家收集整理的关于Python Tkinter清除一个框架的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试清除TKINTER中的一个框架,以便可以编写新内容(刷新信息),但我无法做到这一点.我知道这些

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

但是frame.destroy()将完全删除框架.另外两个也无法给我我想要的结果.我需要的只是清除框架中的每个项目,但框架本身将保持.无论如何有什么可以做的吗?

推荐答案

pack_forget和grid_forget只能从视图中删除小部件,不会摧毁它们.如果您不打算重新使用小部件,那么您唯一的真正选择就是用destroy方法销毁它们.

做到这一点,您有两种选择:单独销毁每个选择,或破坏将导致其所有孩子被摧毁的框架.后者通常是最简单,最有效的.

由于您声称您不想销毁容器框架,因此创建一个辅助框架.让此次级框架是您要删除的所有小部件的容器,然后将此框架放入您要销毁的父母内.然后,这只是摧毁这一框架的问题,所有内部小部件都会随之销毁.

其他推荐答案

/RIN2/book2/2405/docs/tkinter/universal.html

w.winfo_children()
返回所有W的孩子的清单 从最低(底部)到最高(顶部)的堆叠顺序.

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

将破坏框架中的所有小部件.不需要第二帧.

其他推荐答案

对于清晰的框架,首先需要销毁框架内的所有小部件.它将清除框架.

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

本文地址:https://www.itbaoku.cn/post/2091129.html

问题描述

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

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?

推荐答案

pack_forget and grid_forget will 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 destroy method.

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.

其他推荐答案

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

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

Will destroy all the widget in your frame. No need for a second frame.

其他推荐答案

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