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 中是否存在小部件?


- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01