33

Is it possible to get all of the children of a Tkinter widget, then get the children's children etc.?

Basically I want all of the widgets within one entire window.

Edit : I found a solution utilizing Bryan's line :

def all_children (wid) :
    _list = wid.winfo_children()

    for item in _list :
        if item.winfo_children() :
            _list.extend(item.winfo_children())

    return _list
Sam
  • 1,509
  • 3
  • 19
  • 28
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
  • For searchers (such as myself) the key word is "recursively", eg "How do I recursively iterate over all children of a widget." – tex Nov 01 '15 at 14:04
  • wont reading and adding into the same list cause for loop to iterate forever.? – Faraaz Kurawle Jul 20 '23 at 09:14

1 Answers1

36

The method you are looking for is winfo_children.

Recursive like this:

def all_children(wid, finList=None, indent=0):
    finList = finList or []
    print(f"{'   ' * indent}{wid=}")
    children = wid.winfo_children()
    for item in children:
        finList.append(item)
        all_children(item, finList, indent + 1)
    return finList
Volker Siegel
  • 3,277
  • 2
  • 24
  • 35
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 2
    Bryan's link no longer is active try this one instead [Every Child](https://www.tutorialspoint.com/getting-every-child-widget-of-a-tkinter-window) – Jim Robinson Dec 09 '21 at 04:54