madprogramer madprogramer - 4 months ago 54
Python Question

How to see if a widget exists in Tkinter?

Now, I know that you can check to see if a window exists by:


Which returns a Boolean. Now I have searched but haven't been able to find exactly what I need. More specifically I need to check the existence of my buttons, labels, list boxes, sliders etc.


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()
>>> root.winfo_children()
[<Tkinter.Label instance at 0x0000000002ADC1C8>]
>>> label.destroy()
>>> label.winfo_exists()
>>> root.winfo_children()