| 1 |
# Copyright (c) Twisted Matrix Laboratories. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
"""Demo of wxPython integration with Twisted.""" |
| 5 |
|
| 6 |
|
| 7 |
import sys |
| 8 |
|
| 9 |
from wx import EVT_CLOSE, EVT_MENU, App, DefaultPosition, Frame, Menu, MenuBar, Size |
| 10 |
|
| 11 |
from twisted.internet import wxreactor |
| 12 |
from twisted.python import log |
| 13 |
|
| 14 |
wxreactor.install() |
| 15 |
|
| 16 |
# import t.i.reactor only after installing wxreactor: |
| 17 |
from twisted.internet import reactor |
| 18 |
|
| 19 |
ID_EXIT = 101 |
| 20 |
|
| 21 |
|
| 22 |
class MyFrame(Frame): |
| 23 |
def __init__(self, parent, ID, title): |
| 24 |
Frame.__init__(self, parent, ID, title, DefaultPosition, Size(300, 200)) |
| 25 |
menu = Menu() |
| 26 |
menu.Append(ID_EXIT, "E&xit", "Terminate the program") |
| 27 |
menuBar = MenuBar() |
| 28 |
menuBar.Append(menu, "&File") |
| 29 |
self.SetMenuBar(menuBar) |
| 30 |
EVT_MENU(self, ID_EXIT, self.DoExit) |
| 31 |
|
| 32 |
# make sure reactor.stop() is used to stop event loop: |
| 33 |
EVT_CLOSE(self, lambda evt: reactor.stop()) |
| 34 |
|
| 35 |
def DoExit(self, event): |
| 36 |
reactor.stop() |
| 37 |
|
| 38 |
|
| 39 |
class MyApp(App): |
| 40 |
def twoSecondsPassed(self): |
| 41 |
print("two seconds passed") |
| 42 |
|
| 43 |
def OnInit(self): |
| 44 |
frame = MyFrame(None, -1, "Hello, world") |
| 45 |
frame.Show(True) |
| 46 |
self.SetTopWindow(frame) |
| 47 |
# look, we can use twisted calls! |
| 48 |
reactor.callLater(2, self.twoSecondsPassed) |
| 49 |
return True |
| 50 |
|
| 51 |
|
| 52 |
def demo(): |
| 53 |
log.startLogging(sys.stdout) |
| 54 |
|
| 55 |
# register the App instance with Twisted: |
| 56 |
app = MyApp(0) |
| 57 |
reactor.registerWxApp(app) |
| 58 |
|
| 59 |
# start the event loop: |
| 60 |
reactor.run() |
| 61 |
|
| 62 |
|
| 63 |
if __name__ == "__main__": |
| 64 |
demo() |