]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/OOR.py
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 EVT_BUTTON(self
, BTN1
, self
.OnFindButton1
)
36 EVT_BUTTON(self
, BTN2
, self
.OnFindButton2
)
39 def OnFindButton1(self
, evt
):
40 win
= self
.FindWindowById(BTN1
)
42 self
.log
.write("***** OOPS! None returned...\n")
44 className
= win
.__class
__.__name
__
45 if className
in ["wxButton", "wxButtonPtr"]:
46 self
.log
.write("The types are the same! <grin>\n")
48 self
.log
.write("Got %s, expected wxButton or wxButtonPtr\n" % className
)
52 def OnFindButton2(self
, evt
):
53 win
= self
.FindWindowById(BTN2
)
55 self
.log
.write("***** OOPS! None returned...\n")
58 self
.log
.write("The objects are the same! <grin>\n")
60 self
.log
.write("The objects are NOT the same! <frown>\n")
62 win
= evt
.GetEventObject()
64 self
.log
.write("***** OOPS! None returned...\n")
67 self
.log
.write("The objects are the same! <grin>\n")
69 self
.log
.write("The objects are NOT the same! <frown>\n")
72 #----------------------------------------------------------------------
74 def runTest(frame
, nb
, log
):
75 win
= TestPanel(nb
, log
)
78 #----------------------------------------------------------------------
83 <h2>Original Object Return</h2>
85 <p>Several methods in wxWindows return pointers to base class objects,
86 when in fact the actual object pointed to is of a derived type. Since
87 SWIG isn't able to tell the actual type it just creates a new Python
88 shadow object of the base type to wrap around the base type pointer
91 <p>In wxPython prior to 2.3.0 this could cause annoying issues. For
92 example if you called:
96 myText = someWindow.FindWindowById(txtID)
99 expecting to get a wxTextCtrl you would actually get a wxWindow object
100 instead. If you then try to call SetValue on that object you'll get
101 an exception since there is no such method. This is the reason for
102 the wxPyTypeCast hack that has been in wxPython for so long.
104 <p>Even with wxPyTypeCast there was the issue that the object returned
105 was not the same one that was created in Python originally, but a new
106 object of the same type that wraps the same C++ pointer. If the
107 programmer has set additional attributes of that original object they
108 will not exist in the new object.
110 <p>For a long time now I have wanted to do away with wxPyTypeCast and
111 also find a way to return the original Python object from methods like
112 FindWindowById. This project naturally divides into two phases:
116 <li>Teach the wrapper methods how to return objects of the right type,
117 and be able to then turn wxPyTypeCast in to a no-op.
119 <li>Be able to return the original Python shadow object if it still exists.
123 <p>The first button below shows the first of these phases (<i>working</i>)
124 and the second will show #2 (<i>working as of 2.3.2</i>)