]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/pyTree.py
2 Hello, and welcome to this test of the wxTreeItemData
5 The wxTreeItemData class can be used to associate a python
6 object with a wxTreeCtrl item. In this sample, its use is
7 demonstrated via a tree control that shows the contents of a
8 python namespace according to the standard dir()
9 command. Every item in the tree has its label taken from the
10 dir() output, and 'behind it' a reference to the python
11 object is stored in a wxTreeItemData object.
13 As you may have guessed by now, this sample automatically
14 displays '__doc__' strings if the selected python object
15 happens to have one. Please expand the pyTree object to
16 learn more about the implementation.
18 Version 1.0, April 4 1999.
19 Harm van der Heijden (H.v.d.Heijden@phys.tue.nl)
21 P.S. Check out the string module. It's imported in this
22 sample not because it's used, but because it's so
23 beautifully documented...
26 from wxPython
import wx
27 import string
# Don't use it, but it's fun expanding :-)
29 #----------------------------------------------------------------------
32 """Returns the indentation level of the given line."""
35 if c
== ' ': indent
= indent
+ 1
36 elif c
== '\t': indent
= indent
+ 8
40 def _sourcefinder(func
):
41 """Given a func_code object, this function tries to find and return
42 the python source code of the function."""
44 f
= open(func
.co_filename
,"r")
46 return "(could not open file %s)" % (func
.co_filename
,)
48 for i
in range(func
.co_firstlineno
):
50 ind
= _getindent(line
)
55 # the following should be <= ind, but then we get
56 # confused by multiline docstrings. Using == works most of
57 # the time... but not always!
58 if _getindent(line
) == ind
: break
61 #----------------------------------------------------------------------
63 class pyTree(wx
.wxTreeCtrl
):
65 This wxTreeCtrl derivative displays a tree view of a Python namespace.
66 Anything from which the dir() command returns a non-empty list is a branch
70 def __init__(self
, parent
, id, root
):
72 Initialize function; because we insert branches into the tree
73 as needed, we use the ITEM_EXPANDING event handler. The
74 ITEM_COLLAPSED handler removes the stuff afterwards. The
75 SEL_CHANGED handler attempts to display interesting
76 information about the selected object.
78 wx
.wxTreeCtrl
.__init
__(self
, parent
, id)
79 self
.root
= self
.AddRoot(str(root
), -1, -1, wx
.wxTreeItemData(root
))
81 self
.SetItemHasChildren(self
.root
, wx
.TRUE
)
82 wx
.EVT_TREE_ITEM_EXPANDING(self
, self
.GetId(), self
.OnItemExpanding
)
83 wx
.EVT_TREE_ITEM_COLLAPSED(self
, self
.GetId(), self
.OnItemCollapsed
)
84 wx
.EVT_TREE_SEL_CHANGED(self
, self
.GetId(), self
.OnSelChanged
)
88 def SetOutput(self
, output
):
90 Set output function (accepts single string). Used to display string
91 representation of the selected object by OnSelChanged.
96 def OnItemExpanding(self
,event
):
98 The real workhorse of this class. First we retrieve the object
99 (parent) belonging to the branch that is to be expanded. This
100 is done by calling GetPyData(parent), which is a short-cut for
101 GetPyItemData(parent).Get().
103 Then we get the dir() list of that object. For each item in
104 this list, a tree item is created with associated
105 wxTreeItemData referencing the child object. We get this
106 object using child = getattr(parent, item).
108 Finally, we check wether the child returns a non-empty dir()
109 list. If so, it is labeled as 'having children', so that it
110 may be expanded. When it actually is expanded, this function
111 will again figure out what the offspring is.
113 item
= event
.GetItem()
114 obj
= self
.GetPyData( item
)
117 new_obj
= getattr(obj
,key
)
118 new_item
= self
.AppendItem( item
, key
, -1, -1,
119 wx
.wxTreeItemData(new_obj
) )
121 self
.SetItemHasChildren(new_item
, wx
.TRUE
)
123 def OnItemCollapsed(self
, event
):
125 We need to remove all children here, otherwise we'll see all
126 that old rubbish again after the next expansion.
128 item
= event
.GetItem()
129 self
.DeleteChildren(item
)
131 def OnSelChanged(self
, event
):
133 If an output function is defined, we try to print some
134 informative, interesting and thought-provoking stuff to it.
135 If it has a __doc__ string, we print it. If it's a function or
136 unbound class method, we attempt to find the python source.
140 obj
= self
.GetPyData( event
.GetItem() )
142 if hasattr(obj
, '__doc__'):
143 msg
= msg
+"\n\nDocumentation string:\n\n%s" % ( getattr(obj
, '__doc__'),)
146 if hasattr(obj
, "func_code"): # normal function
147 func
= getattr(obj
, "func_code")
148 elif hasattr(obj
, "im_func"): # unbound class method
149 func
= getattr(getattr(obj
, "im_func"), "func_code")
150 if func
: # if we found one, let's try to print the source
151 msg
= msg
+"\n\nFunction source:\n\n" + _sourcefinder(func
)
153 apply(self
.output
, (msg
,))
155 #----------------------------------------------------------------------
159 def runTest(frame
, nb
, log
):
161 This method is used by the wxPython Demo Framework for integrating
162 this demo with the rest.
164 thisModule
= __import__(__name__
, globals())
165 win
= wx
.wxFrame(frame
, -1, "PyTreeItemData Test")
166 split
= wx
.wxSplitterWindow(win
, -1)
167 tree
= pyTree(split
, -1, thisModule
)
168 text
= wx
.wxTextCtrl(split
, -1, "", wx
.wxDefaultPosition
,
169 wx
.wxDefaultSize
, wx
.wxTE_MULTILINE
)
170 split
.SplitVertically(tree
, text
, 200)
171 tree
.SetOutput(text
.SetValue
)
172 tree
.SelectItem(tree
.root
)
173 win
.SetSize(wx
.wxSize(800,500))
179 #----------------------------------------------------------------------
180 if __name__
== '__main__':
182 class MyFrame(wx
.wxFrame
):
183 """Very standard Frame class. Nothing special here!"""
186 """Make a splitter window; left a tree, right a textctrl. Wow."""
188 wx
.wxFrame
.__init
__(self
, wx
.NULL
, -1, "PyTreeItemData Test",
189 wx
.wxDefaultPosition
, wx
.wxSize(800,500))
190 split
= wx
.wxSplitterWindow(self
, -1)
191 tree
= pyTree(split
, -1, __main__
)
192 text
= wx
.wxTextCtrl(split
, -1, "", wx
.wxDefaultPosition
,
193 wx
.wxDefaultSize
, wx
.wxTE_MULTILINE
)
194 split
.SplitVertically(tree
, text
, 200)
195 tree
.SetOutput(text
.SetValue
)
196 tree
.SelectItem(tree
.root
)
198 class MyApp(wx
.wxApp
):
199 """This class is even less interesting than MyFrame."""
202 """OnInit. Boring, boring, boring!"""
205 self
.SetTopWindow(frame
)