Example:

   1 from Tkinter import *
   2  
   3 class WidgetWalker:
   4     """when called WidgetWalker will recursivly visit 
   5     all children of the given parent container
   6     """
   7     def __init__(self, parent):
   8         """parent is the container widget that we will start visiting"""
   9         self.parent = parent
  10  
  11     def __call__(self):
  12         """start walking"""
  13         self.walk(self.parent)
  14  
  15     def update(self, widget):
  16         """called for every widget found
  17 
  18         Not all widgets have the same options so wrap 
  19         updates in try: except TclError: 
  20         """
  21  
  22         try:
  23             widget["font"] = "Helvetica 18"
  24         except TclError:
  25             pass
  26  
  27         try:
  28             widget["fg"] = "pink"
  29         except TclError:
  30             pass
  31  
  32     def walk(self, top):
  33         """call update then start recursing through 
  34         the widgets children
  35         
  36         children is a dictionary {name : instance} 
  37         all Tkinter Widgets have.
  38         """
  39         self.update(top)
  40         for child in top.children.values():
  41             self.walk(child)
  42  
  43  
  44  
  45  
  46 if __name__=="__main__":
  47     root = Tk()
  48     f = Frame(root)
  49     l = Label(f, text="Label")
  50     l.pack()
  51     b = Button(f, text="Button", command=WidgetWalker(root))
  52     b.pack()
  53     inf = Frame(f)
  54     l2 = Label(inf, text="Label 2")
  55     l2.pack()
  56     inf2 = Frame(inf)
  57     l3 = Label(inf2, text="Label 3")
  58     l3.pack()
  59     inf2.pack()
  60     inf.pack()
  61     f.pack()
  62     root.mainloop()
  63 

Listing All Widgets in Hierarchy

Here's related code; It lists all widgets hierarchically.

It's Python 3.x code.

   1 def hierarchy(widget):
   2     """Return the widget hierarchy (from widget, down) as nested dicts.
   3     Each dict is as follows:
   4     {"path": path,
   5      "name": name,
   6      "id": (integer id),
   7      "widget": (widget),
   8      "class": class_string,
   9      "children": (...,
  10                   ...)}
  11     """
  12     _id = widget.winfo_id()
  13     return {"path": widget.winfo_pathname(_id),
  14             "name": widget.winfo_name(),
  15             "id": _id,
  16             "widget": widget,
  17             "class": widget.winfo_class(),
  18             "children":
  19                 tuple([hierarchy(w)
  20                        for w in widget.winfo_children()])}
  21 
  22 def print_hierarchy(widget):
  23     """Print the hierarchy of a widget.
  24 
  25     All widgets are shown in the hierarchy;
  26     WANTED: The widget itself is surrounded with a [[]] marker.
  27     """
  28     def print_partial(info, depth):
  29         spaces = "  "*depth
  30         content = "{name} [{id}] <{class}>".format(**info)
  31         print(spaces, end="")
  32         if info["widget"] == widget:
  33             print("** {} **".format(content))
  34         else:
  35             print(content)
  36         for child in info["children"]:
  37             print_partial(child, depth+1)
  38     print_partial(hierarchy(widget.nametowidget(".")), 0)
  39 

tkinter: WidgetWalker (last edited 2011-05-28 09:15:16 by LionKimbro)