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