3 # - support partial or total redisplay
10 # - for classes/modules, add "open source" to object browser
12 from TreeWidget
import TreeItem
, TreeNode
, ScrolledCanvas
17 myrepr
.maxstring
= 100
20 class ObjectTreeItem(TreeItem
):
21 def __init__(self
, labeltext
, object, setfunction
=None):
22 self
.labeltext
= labeltext
24 self
.setfunction
= setfunction
25 def GetLabelText(self
):
28 return myrepr
.repr(self
.object)
29 def GetIconName(self
):
30 if not self
.IsExpandable():
33 return self
.setfunction
is not None
34 def SetText(self
, text
):
37 self
.setfunction(value
)
42 def IsExpandable(self
):
43 return not not dir(self
.object)
45 keys
= dir(self
.object)
49 value
= getattr(self
.object, key
)
50 except AttributeError:
52 item
= make_objecttreeitem(
55 lambda value
, key
=key
, object=self
.object:
56 setattr(object, key
, value
))
60 class InstanceTreeItem(ObjectTreeItem
):
61 def IsExpandable(self
):
64 sublist
= ObjectTreeItem
.GetSubList(self
)
66 make_objecttreeitem("__class__ =", self
.object.__class
__))
69 class ClassTreeItem(ObjectTreeItem
):
70 def IsExpandable(self
):
73 sublist
= ObjectTreeItem
.GetSubList(self
)
74 if len(self
.object.__bases
__) == 1:
75 item
= make_objecttreeitem("__bases__[0] =",
76 self
.object.__bases
__[0])
78 item
= make_objecttreeitem("__bases__ =", self
.object.__bases
__)
79 sublist
.insert(0, item
)
82 class AtomicObjectTreeItem(ObjectTreeItem
):
83 def IsExpandable(self
):
86 class SequenceTreeItem(ObjectTreeItem
):
87 def IsExpandable(self
):
88 return len(self
.object) > 0
90 return range(len(self
.object))
93 for key
in self
.keys():
95 value
= self
.object[key
]
98 def setfunction(value
, key
=key
, object=self
.object):
100 item
= make_objecttreeitem("%r:" % (key
,), value
, setfunction
)
104 class DictTreeItem(SequenceTreeItem
):
106 keys
= self
.object.keys()
116 IntType
: AtomicObjectTreeItem
,
117 LongType
: AtomicObjectTreeItem
,
118 FloatType
: AtomicObjectTreeItem
,
119 StringType
: AtomicObjectTreeItem
,
120 TupleType
: SequenceTreeItem
,
121 ListType
: SequenceTreeItem
,
122 DictType
: DictTreeItem
,
123 InstanceType
: InstanceTreeItem
,
124 ClassType
: ClassTreeItem
,
127 def make_objecttreeitem(labeltext
, object, setfunction
=None):
133 return c(labeltext
, object, setfunction
)
139 from Tkinter
import Tk
141 root
.configure(bd
=0, bg
="yellow")
143 sc
= ScrolledCanvas(root
, bg
="white", highlightthickness
=0, takefocus
=1)
144 sc
.frame
.pack(expand
=1, fill
="both")
145 item
= make_objecttreeitem("sys", sys
)
146 node
= TreeNode(sc
.canvas
, None, item
)
150 if __name__
== '__main__':