2 #----------------------------------------------------------------------
4 class PyOnDemandOutputWindow
:
6 A class that can be used for redirecting Python's stdout and
7 stderr streams. It will do nothing until something is wrriten to
8 the stream at which point it will create a Frame with a text area
9 and write the text there.
11 def __init__(self
, title
= "wxPython: stdout/stderr"):
16 def SetParent(self
, parent
):
17 """Set the window to be used as the popup Frame's parent."""
21 def CreateOutputWindow(self
, st
):
22 self
.frame
= wx
.Frame(self
.parent
, -1, self
.title
,
23 style
=wx
.DEFAULT_FRAME_STYLE | wx
.NO_FULL_REPAINT_ON_RESIZE
)
24 self
.text
= wxTextCtrl(self
.frame
, -1, "",
25 style
= wx
.TE_MULTILINE | wx
.TE_READONLY
)
26 self
.frame
.SetSize((450, 300))
28 EVT_CLOSE(self
.frame
, self
.OnCloseWindow
)
31 # These methods provide the file-like output behaviour.
32 def write(self
, text
):
34 Create the output window if needed and write the string to it.
35 If not called in the context of the gui thread then uses
36 CallAfter to do the work there.
38 if self
.frame
is None:
39 if not wx
.Thread_IsMain():
40 wx
.CallAfter(self
.CreateOutputWindow
, text
)
42 self
.CreateOutputWindow(text
)
44 if not wx
.Thread_IsMain():
45 wx
.CallAfter(self
.text
.AppendText
, text
)
47 self
.text
.AppendText(text
)
51 if self
.frame
is not None:
52 wx
.CallAfter(self
.frame
.Close
)
55 def OnCloseWindow(self
, event
):
56 if self
.frame
is not None:
61 #----------------------------------------------------------------------
63 _defRedirect
= (wx
.Platform
== '__WXMSW__' or wx
.Platform
== '__WXMAC__')
67 The main application class. Derive from this and implement an OnInit
68 method that creates a frame and then calls self.SetTopWindow(frame)
70 outputWindowClass
= PyOnDemandOutputWindow
72 def __init__(self
, redirect
=_defRedirect
, filename
=None, useBestVisual
=False):
73 wx
.PyApp
.__init
__(self
)
75 if wx
.Platform
== "__WXMAC__":
78 if not MacOS
.WMAvailable():
80 This program needs access to the screen. Please run with 'pythonw',
81 not 'python', and only when you are logged in on the main display of
87 # This has to be done before OnInit
88 self
.SetUseBestVisual(useBestVisual
)
90 # Set the default handler for SIGINT. This fixes a problem
91 # where if Ctrl-C is pressed in the console that started this
92 # app then it will not appear to do anything, (not even send
93 # KeyboardInterrupt???) but will later segfault on exit. By
94 # setting the default handler then the app will exit, as
95 # expected (depending on platform.)
98 signal
.signal(signal
.SIGINT
, signal
.SIG_DFL
)
102 # Save and redirect the stdio to a window?
104 self
.saveStdio
= (_sys
.stdout
, _sys
.stderr
)
106 self
.RedirectStdio(filename
)
108 # This finishes the initialization of wxWindows and then calls
109 # the OnInit that should be present in the derived class
115 self
.RestoreStdio() # Just in case the MainLoop was overridden
120 def SetTopWindow(self
, frame
):
122 self
.stdioWin
.SetParent(frame
)
123 wx
.PyApp
.SetTopWindow(self
, frame
)
127 wx
.PyApp
.MainLoop(self
)
131 def RedirectStdio(self
, filename
):
133 _sys
.stdout
= _sys
.stderr
= open(filename
, 'a')
135 self
.stdioWin
= self
.outputWindowClass()
136 _sys
.stdout
= _sys
.stderr
= self
.stdioWin
139 def RestoreStdio(self
):
140 _sys
.stdout
, _sys
.stderr
= self
.saveStdio
144 # change from wxPyApp_ to wxApp_
145 App_GetMacSupportPCMenuShortcuts
= _core
.PyApp_GetMacSupportPCMenuShortcuts
146 App_GetMacAboutMenuItemId
= _core
.PyApp_GetMacAboutMenuItemId
147 App_GetMacPreferencesMenuItemId
= _core
.PyApp_GetMacPreferencesMenuItemId
148 App_GetMacExitMenuItemId
= _core
.PyApp_GetMacExitMenuItemId
149 App_GetMacHelpMenuTitleName
= _core
.PyApp_GetMacHelpMenuTitleName
150 App_SetMacSupportPCMenuShortcuts
= _core
.PyApp_SetMacSupportPCMenuShortcuts
151 App_SetMacAboutMenuItemId
= _core
.PyApp_SetMacAboutMenuItemId
152 App_SetMacPreferencesMenuItemId
= _core
.PyApp_SetMacPreferencesMenuItemId
153 App_SetMacExitMenuItemId
= _core
.PyApp_SetMacExitMenuItemId
154 App_SetMacHelpMenuTitleName
= _core
.PyApp_SetMacHelpMenuTitleName
155 App_GetComCtl32Version
= _core
.PyApp_GetComCtl32Version
157 #----------------------------------------------------------------------------
159 class PySimpleApp(wx
.App
):
161 A simple application class. You can just create one of these and
162 then then make your top level windows later, and not have to worry
165 def __init__(self
, redirect
=False, filename
=None, useBestVisual
=False):
166 wx
.App
.__init
__(self
, redirect
, filename
, useBestVisual
)
169 wx
.InitAllImageHandlers()
173 # Is anybody using this one?
174 class PyWidgetTester(wx
.App
):
175 def __init__(self
, size
= (250, 100)):
177 wx
.App
.__init
__(self
, 0)
180 self
.frame
= wxFrame(None, -1, "Widget Tester", pos
=(0,0), size
=self
.size
)
181 self
.SetTopWindow(self
.frame
)
184 def SetWidget(self
, widgetClass
, *args
):
185 w
= widgetClass(self
.frame
, *args
)
186 self
.frame
.Show(True)
188 #----------------------------------------------------------------------------
189 # DO NOT hold any other references to this object. This is how we
190 # know when to cleanup system resources that wxWin is holding. When
191 # the sys module is unloaded, the refcount on sys.__wxPythonCleanup
192 # goes to zero and it calls the wxApp_CleanUp function.
196 self
.cleanup
= _core
.App_CleanUp
200 _sys
.__wxPythonCleanup
= __wxPyCleanup()
202 ## # another possible solution, but it gets called too early...
203 ## if sys.version[0] == '2':
205 ## atexit.register(_core.wxApp_CleanUp)
207 ## sys.exitfunc = _core.wxApp_CleanUp
210 #----------------------------------------------------------------------------