]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/OOR.py
1 # 11/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
7 import wx
.html
as wxhtml
9 #----------------------------------------------------------------------
14 class TestPanel(wx
.Panel
):
15 def __init__(self
, parent
, log
):
16 wx
.Panel
.__init
__(self
, parent
, -1)
19 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
20 html
= wxhtml
.HtmlWindow(self
, -1)
21 html
.SetPage(overview
)
22 sizer
.Add(html
, 1, wx
.EXPAND|wx
.ALL
, 5)
24 btns
= wx
.BoxSizer(wx
.HORIZONTAL
)
25 btns
.Add((50, -1), 1, wx
.EXPAND
)
26 btn1
= wx
.Button(self
, BTN1
, "Find My Alter-ego") # don't save a ref to this one
28 btns
.Add((50, -1), 1, wx
.EXPAND
)
29 self
.btn2
= wx
.Button(self
, BTN2
, "Find Myself")
31 btns
.Add((50, -1), 1, wx
.EXPAND
)
33 sizer
.Add(btns
, 0, wx
.EXPAND|wx
.ALL
, 15)
36 self
.SetAutoLayout(True)
38 self
.sizer
= sizer
# save it for testing later
40 self
.Bind(wx
.EVT_BUTTON
, self
.OnFindButton1
, id=BTN1
)
41 self
.Bind(wx
.EVT_BUTTON
, self
.OnFindButton2
, id=BTN2
)
44 def OnFindButton1(self
, evt
):
45 win
= self
.FindWindowById(BTN1
)
48 self
.log
.write("***** OOPS! None returned...\n")
51 className
= win
.__class
__.__name
__
53 if className
in ["Button", "ButtonPtr"]:
54 self
.log
.write("The types are the same! <grin>\n")
56 self
.log
.write("Got %s, expected wxButton or wxButtonPtr\n" % className
)
60 def OnFindButton2(self
, evt
):
61 win
= self
.FindWindowById(BTN2
)
64 self
.log
.write("***** OOPS! None returned...\n")
68 self
.log
.write("The objects are the same! <grin>\n")
70 self
.log
.write("The objects are NOT the same! <frown>\n")
72 win
= evt
.GetEventObject()
75 self
.log
.write("***** OOPS! None returned...\n")
79 self
.log
.write("The objects are the same! <grin>\n")
81 self
.log
.write("The objects are NOT the same! <frown>\n")
83 sizer
= self
.GetSizer()
86 self
.log
.write("***** OOPS! None returned...\n")
89 if sizer
is self
.sizer
:
90 self
.log
.write("The objects are the same! <grin>\n")
92 self
.log
.write("The objects are NOT the same! <frown>\n")
95 #----------------------------------------------------------------------
97 def runTest(frame
, nb
, log
):
98 win
= TestPanel(nb
, log
)
101 #----------------------------------------------------------------------
106 <h2>Original Object Return</h2>
108 <p>Several methods in wxWindows return pointers to base class objects,
109 when in fact the actual object pointed to is of a derived type. Since
110 SWIG isn't able to tell the actual type it just creates a new Python
111 shadow object of the base type to wrap around the base type pointer
114 <p>In wxPython prior to 2.3.0 this could cause annoying issues. For
115 example if you called:
119 myText = someWindow.FindWindowById(txtID)
122 expecting to get a wxTextCtrl you would actually get a wxWindow object
123 instead. If you then try to call SetValue on that object you'll get
124 an exception since there is no such method. This is the reason for
125 the wxPyTypeCast hack that has been in wxPython for so long.
127 <p>Even with wxPyTypeCast there was the issue that the object returned
128 was not the same one that was created in Python originally, but a new
129 object of the same type that wraps the same C++ pointer. If the
130 programmer has set additional attributes of that original object they
131 will not exist in the new object.
133 <p>For a long time now I have wanted to do away with wxPyTypeCast and
134 also find a way to return the original Python object from methods like
135 FindWindowById. This project naturally divides into two phases:
139 <li>Teach the wrapper methods how to return objects of the right type,
140 and be able to then turn wxPyTypeCast in to a no-op.
142 <li>Be able to return the original Python shadow object if it still exists.
146 <p>The first button below shows the first of these phases (<i>working</i>)
147 and the second will show #2 (<i>working as of Python 2.3.2</i>)
155 if __name__
== '__main__':
158 run
.main(['', os
.path
.basename(sys
.argv
[0])])