]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/src/_core_ex.py
1 #----------------------------------------------------------------------------
3 # Use Python's bool constants if available, make some if not
7 __builtins__
.True = 1==1
8 __builtins__
.False = 1==0
12 # workarounds for bad wxRTTI names
13 __wxPyPtrTypeMap
['wxGauge95'] = 'wxGauge'
14 __wxPyPtrTypeMap
['wxSlider95'] = 'wxSlider'
15 __wxPyPtrTypeMap
['wxStatusBar95'] = 'wxStatusBar'
18 #----------------------------------------------------------------------------
19 # Load version numbers from __version__... Ensure that major and minor
20 # versions are the same for both wxPython and wxWindows.
22 from __version__
import *
23 __version__
= VERSION_STRING
25 assert MAJOR_VERSION
== _core
.MAJOR_VERSION
, "wxPython/wxWindows version mismatch"
26 assert MINOR_VERSION
== _core
.MINOR_VERSION
, "wxPython/wxWindows version mismatch"
27 if RELEASE_VERSION
!= _core
.RELEASE_VERSION
:
29 warnings
.warn("wxPython/wxWindows release number mismatch")
31 #----------------------------------------------------------------------------
33 class PyDeadObjectError(AttributeError):
36 class _wxPyDeadObject(object):
38 Instances of wx objects that are OOR capable will have their __class__
39 changed to this class when the C++ object is deleted. This should help
40 prevent crashes due to referencing a bogus C++ pointer.
42 reprStr
= "wxPython wrapper for DELETED %s object! (The C++ object no longer exists.)"
43 attrStr
= "The C++ part of the %s object has been deleted, attribute access no longer allowed."
46 if not hasattr(self
, "_name"):
47 self
._name
= "[unknown]"
48 return self
.reprStr
% self
._name
50 def __getattr__(self
, *args
):
51 if not hasattr(self
, "_name"):
52 self
._name
= "[unknown]"
53 raise PyDeadObjectError(self
.attrStr
% self
._name
)
55 def __nonzero__(self
):
60 class PyUnbornObjectError(AttributeError):
63 class _wxPyUnbornObject(object):
65 Some stock objects are created when the wx.core module is
66 imported, but their C++ instance is not created until the wx.App
67 object is created and initialized. These object instances will
68 temporarily have their __class__ changed to this class so an
69 exception will be raised if they are used before the C++ instance
73 reprStr
= "wxPython wrapper for UNBORN object! (The C++ object is not initialized yet.)"
74 attrStr
= "The C++ part of this object has not been initialized, attribute access not allowed."
77 #if not hasattr(self, "_name"):
78 # self._name = "[unknown]"
79 return self
.reprStr
#% self._name
81 def __getattr__(self
, *args
):
82 #if not hasattr(self, "_name"):
83 # self._name = "[unknown]"
84 raise PyUnbornObjectError(self
.attrStr
) # % self._name )
86 def __nonzero__(self
):
90 #----------------------------------------------------------------------------
91 _wxPyCallAfterId
= None
93 def CallAfter(callable, *args
, **kw
):
95 Call the specified function after the current and pending event
96 handlers have been completed. This is also good for making GUI
97 method calls from non-GUI threads.
100 assert app
, 'No wxApp created yet'
102 global _wxPyCallAfterId
103 if _wxPyCallAfterId
is None:
104 _wxPyCallAfterId
= wx
.NewEventType()
105 app
.Connect(-1, -1, _wxPyCallAfterId
,
106 lambda event
: event
.callable(*event
.args
, **event
.kw
) )
108 evt
.SetEventType(_wxPyCallAfterId
)
109 evt
.callable = callable
112 wx
.PostEvent(app
, evt
)
115 #----------------------------------------------------------------------------
120 A convenience class for wx.Timer, that calls the given callable
121 object once after the given amount of milliseconds, passing any
122 positional or keyword args. The return value of the callable is
123 availbale after it has been run with the GetResult method.
125 If you don't need to get the return value or restart the timer
126 then there is no need to hold a reference to this object. It will
127 hold a reference to itself while the timer is running (the timer
128 has a reference to self.Notify) but the cycle will be broken when
129 the timer completes, automatically cleaning up the wx.FutureCall
132 def __init__(self
, millis
, callable, *args
, **kwargs
):
134 self
.callable = callable
135 self
.SetArgs(*args
, **kwargs
)
147 def Start(self
, millis
=None, *args
, **kwargs
):
152 if millis
is not None:
155 self
.SetArgs(*args
, **kwargs
)
157 self
.timer
= wx
.PyTimer(self
.Notify
)
158 self
.timer
.Start(self
.millis
, wx
.TIMER_ONE_SHOT
)
165 Stop and destroy the timer.
167 if self
.timer
is not None:
172 def GetInterval(self
):
173 if self
.timer
is not None:
174 return self
.timer
.GetInterval()
180 return self
.timer
is not None and self
.timer
.IsRunning()
183 def SetArgs(self
, *args
, **kwargs
):
185 (Re)set the args passed to the callable object. This is
186 useful in conjunction with Restart if you want to schedule a
187 new call to the same callable object but with different
202 The timer has expired so call the callable.
204 if self
.callable and getattr(self
.callable, 'im_self', True):
207 self
.result
= self
.callable(*self
.args
, **self
.kwargs
)
210 # if it wasn't restarted, then cleanup
211 wx
.CallAfter(self
.Stop
)
214 #----------------------------------------------------------------------------
215 #----------------------------------------------------------------------------
217 # Import other modules in this package that should show up in the
218 # "core" wx namespace
220 from windows
import *
221 from controls
import *
225 # Fixup the stock objects since they can't be used yet. (They will be
226 # restored in wx.PyApp.OnInit.)
227 _core
._wxPyFixStockObjects
()
229 #----------------------------------------------------------------------------
230 #----------------------------------------------------------------------------