How to see if a widget exists in Tkinter?(如何查看 Tkinter 中是否存在小部件?)
问题描述
现在,我知道您可以通过以下方式检查窗口是否存在:
Now, I know that you can check to see if a window exists by:
x.winfo_exists()
返回一个布尔值.我已经搜索过这个,但无法找到我正在寻找的确切内容.更具体地说,我需要检查我的按钮、标签、列表框、滑块等是否存在.
Which returns a Boolean. I have searched for this, but haven't been able to find exactly what I am looking for. More specifically I need to check the existence of my buttons, labels, list boxes, sliders etc.
推荐答案
winfo_exists
返回 1 除非你已经销毁了小部件,在这种情况下它返回 0.这个方法可以在任何小部件类上调用,不仅是 Tk 根或 Toplevels.或者,您可以使用 winfo_children
获取小部件的所有子级:
winfo_exists
returns 1 unless you have destroyed the widget, in which case it returns 0. This method can be called on any widget class, not only the Tk root or Toplevels. Alternatively, you can get all the children of a widget with winfo_children
:
>>> import Tkinter as tk
>>> root = tk.Tk()
>>> label = tk.Label(root, text="Hello, world")
>>> label.winfo_exists()
1
>>> root.winfo_children()
[<Tkinter.Label instance at 0x0000000002ADC1C8>]
>>> label.destroy()
>>> label.winfo_exists()
0
>>> root.winfo_children()
[]
这篇关于如何查看 Tkinter 中是否存在小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何查看 Tkinter 中是否存在小部件?


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01