Debian Codegraph @

docs/core/examples/wxacceptance.py

raw
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Acceptance tests for wxreactor.
6
7 Please test on Linux, Win32 and macOS:
8 1. Startup event is called at startup.
9 2. Scheduled event is called after 2 seconds.
10 3. Shutdown takes 3 seconds, both when quiting from menu and when closing
11 window (e.g. Alt-F4 in metacity). This tests reactor.stop() and
12 wxApp.ExitEventLoop().
13 4. 'hello, world' continues to be printed even when modal dialog is open
14 (use dialog menu item), when menus are held down, when window is being
15 dragged.
16 """
17 import sys
18 import time
19
20 try:
21 from wx import (
22 EVT_MENU,
23 App as wxApp,
24 DefaultPosition as wxDefaultPosition,
25 Frame as wxFrame,
26 Menu as wxMenu,
27 MenuBar as wxMenuBar,
28 MessageDialog as wxMessageDialog,
29 Size as wxSize,
30 )
31 except ImportError:
32 from wxPython.wx import *
33
34 from twisted.internet import wxreactor
35 from twisted.python import log
36
37 wxreactor.install()
38 from twisted.internet import defer, reactor
39
40 # set up so that "hello, world" is printed continuously
41 dc = None
42
43
44 def helloWorld():
45 global dc
46 print("hello, world", time.time())
47 dc = reactor.callLater(0.1, helloWorld)
48
49
50 dc = reactor.callLater(0.1, helloWorld)
51
52
53 def twoSecondsPassed():
54 print("two seconds passed")
55
56
57 def printer(s):
58 print(s)
59
60
61 def shutdown():
62 print("shutting down in 3 seconds")
63 if dc.active():
64 dc.cancel()
65 reactor.callLater(1, printer, "2...")
66 reactor.callLater(2, printer, "1...")
67 reactor.callLater(3, printer, "0...")
68 d = defer.Deferred()
69 reactor.callLater(3, d.callback, 1)
70 return d
71
72
73 def startup():
74 print("Start up event!")
75
76
77 reactor.callLater(2, twoSecondsPassed)
78 reactor.addSystemEventTrigger("after", "startup", startup)
79 reactor.addSystemEventTrigger("before", "shutdown", shutdown)
80
81
82 ID_EXIT = 101
83 ID_DIALOG = 102
84
85
86 class MyFrame(wxFrame):
87 def __init__(self, parent, ID, title):
88 wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(300, 200))
89 menu = wxMenu()
90 menu.Append(ID_DIALOG, "D&ialog", "Show dialog")
91 menu.Append(ID_EXIT, "E&xit", "Terminate the program")
92 menuBar = wxMenuBar()
93 menuBar.Append(menu, "&File")
94 self.SetMenuBar(menuBar)
95 EVT_MENU(self, ID_EXIT, self.DoExit)
96 EVT_MENU(self, ID_DIALOG, self.DoDialog)
97 # you really ought to do this instead of reactor.stop() in
98 # DoExit, but for the sake of testing we'll let closing the
99 # window shutdown wx without reactor.stop(), to make sure that
100 # still does the right thing.
101 # EVT_CLOSE(self, lambda evt: reactor.stop())
102
103 def DoDialog(self, event):
104 dl = wxMessageDialog(
105 self,
106 "Check terminal to see if messages are still being " "printed by Twisted.",
107 )
108 dl.ShowModal()
109 dl.Destroy()
110
111 def DoExit(self, event):
112 reactor.stop()
113
114
115 class MyApp(wxApp):
116 def OnInit(self):
117 frame = MyFrame(None, -1, "Hello, world")
118 frame.Show(True)
119 self.SetTopWindow(frame)
120 return True
121
122
123 def demo():
124 log.startLogging(sys.stdout)
125 app = MyApp(0)
126 reactor.registerWxApp(app)
127 reactor.run()
128
129
130 if __name__ == "__main__":
131 demo()