2 from wxPython
.wx
import *
3 from wxPython
.html
import *
5 #----------------------------------------------------------------------
11 class TestPanel(wxPanel
):
12 def __init__(self
, parent
, log
):
13 wxPanel
.__init
__(self
, parent
, -1)
16 sizer
= wxBoxSizer(wxVERTICAL
)
17 html
= wxHtmlWindow(self
, -1)
18 html
.SetPage(overview
)
19 sizer
.Add(html
, 1, wxEXPAND|wxALL
, 5)
21 btns
= wxBoxSizer(wxHORIZONTAL
)
22 btns
.Add(50, -1, 1, wxEXPAND
)
23 btn1
= wxButton(self
, BTN1
, "Find My Alter-ego") # don't save a ref to this one
25 btns
.Add(50, -1, 1, wxEXPAND
)
26 self
.btn2
= wxButton(self
, BTN2
, "Find Myself")
28 btns
.Add(50, -1, 1, wxEXPAND
)
30 sizer
.Add(btns
, 0, wxEXPAND|wxALL
, 15)
33 self
.SetAutoLayout(true
)
35 self
.sizer
= sizer
# save it for testing later
37 EVT_BUTTON(self
, BTN1
, self
.OnFindButton1
)
38 EVT_BUTTON(self
, BTN2
, self
.OnFindButton2
)
41 def OnFindButton1(self
, evt
):
42 win
= self
.FindWindowById(BTN1
)
44 self
.log
.write("***** OOPS! None returned...\n")
46 className
= win
.__class
__.__name
__
47 if className
in ["wxButton", "wxButtonPtr"]:
48 self
.log
.write("The types are the same! <grin>\n")
50 self
.log
.write("Got %s, expected wxButton or wxButtonPtr\n" % className
)
54 def OnFindButton2(self
, evt
):
55 win
= self
.FindWindowById(BTN2
)
57 self
.log
.write("***** OOPS! None returned...\n")
60 self
.log
.write("The objects are the same! <grin>\n")
62 self
.log
.write("The objects are NOT the same! <frown>\n")
64 win
= evt
.GetEventObject()
66 self
.log
.write("***** OOPS! None returned...\n")
69 self
.log
.write("The objects are the same! <grin>\n")
71 self
.log
.write("The objects are NOT the same! <frown>\n")
73 sizer
= self
.GetSizer()
75 self
.log
.write("***** OOPS! None returned...\n")
77 if sizer
is self
.sizer
:
78 self
.log
.write("The objects are the same! <grin>\n")
80 self
.log
.write("The objects are NOT the same! <frown>\n")
83 #----------------------------------------------------------------------
85 def runTest(frame
, nb
, log
):
86 win
= TestPanel(nb
, log
)
89 #----------------------------------------------------------------------
94 <h2>Original Object Return</h2>
96 <p>Several methods in wxWindows return pointers to base class objects,
97 when in fact the actual object pointed to is of a derived type. Since
98 SWIG isn't able to tell the actual type it just creates a new Python
99 shadow object of the base type to wrap around the base type pointer
102 <p>In wxPython prior to 2.3.0 this could cause annoying issues. For
103 example if you called:
107 myText = someWindow.FindWindowById(txtID)
110 expecting to get a wxTextCtrl you would actually get a wxWindow object
111 instead. If you then try to call SetValue on that object you'll get
112 an exception since there is no such method. This is the reason for
113 the wxPyTypeCast hack that has been in wxPython for so long.
115 <p>Even with wxPyTypeCast there was the issue that the object returned
116 was not the same one that was created in Python originally, but a new
117 object of the same type that wraps the same C++ pointer. If the
118 programmer has set additional attributes of that original object they
119 will not exist in the new object.
121 <p>For a long time now I have wanted to do away with wxPyTypeCast and
122 also find a way to return the original Python object from methods like
123 FindWindowById. This project naturally divides into two phases:
127 <li>Teach the wrapper methods how to return objects of the right type,
128 and be able to then turn wxPyTypeCast in to a no-op.
130 <li>Be able to return the original Python shadow object if it still exists.
134 <p>The first button below shows the first of these phases (<i>working</i>)
135 and the second will show #2 (<i>working as of 2.3.2</i>)