如何查看 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()
[]
相关文章