如何从窗口中删除 Tkinter 小部件? [英] How to delete Tkinter widgets from a window?
本文介绍了如何从窗口中删除 Tkinter 小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个我想要动态更改的 tkinter
小部件列表.
I have a list of tkinter
widgets that I want to change dynamically.
如何从窗口中删除小部件?
How to delete the widgets from the window?
推荐答案
您可以调用pack_forget
删除小部件(如果您使用 pack
将其添加到窗口中).
You can call pack_forget
to remove a widget (if you use pack
to add it to the window).
示例:
from tkinter import *
root = Tk()
b = Button(root, text="Delete me", command=lambda: b.pack_forget())
b.pack()
root.mainloop()
如果您使用 pack_forget
,您可以稍后再次调用 pack
再次显示小部件.如果您想永久删除它,请在小部件上调用 destroy
(然后您将无法重新添加它).
If you use pack_forget
, you can later show the widget again calling pack
again. If you want to permanently delete it, call destroy
on the widget (then you won't be able to re-add it).
如果您使用 grid
方法,您可以使用 grid_forget
或 grid_remove
以隐藏小部件.
If you use the grid
method, you can use grid_forget
or grid_remove
to hide the widget.
这篇关于如何从窗口中删除 Tkinter 小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文