1
# Twisted, the Framework of Your Internet
2
# Copyright (C) 2001 Matthew W. Lefkowitz
4
# This library is free software; you can redistribute it and/or
5
# modify it under the terms of version 2.1 of the GNU Lesser General Public
6
# License as published by the Free Software Foundation.
8
# This library is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# Lesser General Public License for more details.
13
# You should have received a copy of the GNU Lesser General Public
14
# License along with this library; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
This module provides support for Twisted to interact with the wxPython.
20
In order to use this support, simply do the following::
22
| from twisted.internet import wxreactor
25
Then, when your root wxApp has been created::
27
| from twisted.internet import reactor
28
| reactor.registerWxApp(yourApp)
31
Then use twisted.internet APIs as usual. Stop the event loop using
34
IMPORTANT: tests will fail when run under this reactor. This is expected
35
and does not reflect on the reactor's ability to run real applications,
36
I think. Talk to me if you have questions. -- itamar
39
API Stability: unstable
41
Maintainer: U{Itamar Shtull-Trauring<mailto:twisted@itamarst.org>}
44
from twisted.python.runtime import seconds
45
from twisted.python import log
46
from twisted.internet import default
48
from wxPython.wx import wxTimer, wxApp
51
class ReactorTimer(wxTimer):
52
"""Run reactor event loop every millisecond."""
54
# The wx docs promise no better than 1ms and no worse than 1s (!)
55
# Experiments have shown that Linux, gets better than 50K timer
56
# calls per second -- however, Windows only gets around 100
57
# timer calls per second. Caveat coder.
58
def __init__(self, reactor):
59
wxTimer.__init__(self)
60
self.reactor = reactor
64
"""Called every timer interval"""
65
self.reactor.simulate()
68
class DummyApp(wxApp):
74
class WxReactor(default.SelectReactor):
77
wx drives the event loop, and calls Twisted every millisecond, and
78
Twisted then iterates until a ms has passed.
81
def registerWxApp(self, wxapp):
82
"""Register wxApp instance with the reactor."""
86
default.SelectReactor.crash(self)
89
self.wxapp.ExitMainLoop()
91
def run(self, installSignalHandlers=1):
92
if not hasattr(self, "wxapp"):
93
log.msg("registerWxApp() was not called on reactor, this is probably an error.")
94
self.wxapp = DummyApp(0)
95
self.startRunning(installSignalHandlers=installSignalHandlers)
96
self.timer = ReactorTimer(self)
100
"""Run simulation loops and reschedule callbacks.
107
t = 0.01 - (seconds() - start)
111
"""Configure the twisted mainloop to be run inside the wxPython mainloop.
113
reactor = WxReactor()
114
from twisted.internet.main import installReactor
115
installReactor(reactor)
119
__all__ = ['install']