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中从Azure BLOB存储中读取文件 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01