2 #----------------------------------------------------------------------
4 class PyOnDemandOutputWindow
:
5 def __init__(self
, title
= "wxPython: stdout/stderr"):
10 def SetParent(self
, parent
):
13 def OnCloseWindow(self
, event
):
14 if self
.frame
!= None:
19 # These methods provide the file-like output behaviour.
21 if not wx
.Thread_IsMain():
22 # Aquire the GUI mutex before making GUI calls. Mutex is released
23 # when locker is deleted at the end of this function.
24 locker
= wx
.MutexGuiLocker()
27 self
.frame
= wx
.Frame(self
.parent
, -1, self
.title
,
28 style
=wx
.DEFAULT_FRAME_STYLE | wx
.NO_FULL_REPAINT_ON_RESIZE
)
29 self
.text
= wxTextCtrl(self
.frame
, -1, "",
30 style
= wx
.TE_MULTILINE | wx
.TE_READONLY
)
31 self
.frame
.SetSize((450, 300))
33 EVT_CLOSE(self
.frame
, self
.OnCloseWindow
)
34 self
.text
.AppendText(str)
37 if self
.frame
!= None:
38 if not wx
.Thread_IsMain():
39 locker
= wx
.MutexGuiLocker()
43 #----------------------------------------------------------------------
44 # The main application class. Derive from this and implement an OnInit
45 # method that creates a frame and then calls self.SetTopWindow(frame)
47 _defRedirect
= (wx
.Platform
== '__WXMSW__' or wx
.Platform
== '__WXMAC__')
50 outputWindowClass
= PyOnDemandOutputWindow
52 def __init__(self
, redirect
=_defRedirect
, filename
=None, useBestVisual
=False):
53 wx
.PyApp
.__init
__(self
)
55 if wx
.Platform
== "__WXMAC__":
58 if not MacOS
.WMAvailable():
60 This program needs access to the screen. Please run with 'pythonw',
61 not 'python', and only when you are logged in on the main display of
67 # This has to be done before OnInit
68 self
.SetUseBestVisual(useBestVisual
)
70 # Set the default handler for SIGINT. This fixes a problem
71 # where if Ctrl-C is pressed in the console that started this
72 # app then it will not appear to do anything, (not even send
73 # KeyboardInterrupt???) but will later segfault on exit. By
74 # setting the default handler then the app will exit, as
75 # expected (depending on platform.)
78 signal
.signal(signal
.SIGINT
, signal
.SIG_DFL
)
82 # Save and redirect the stdio to a window?
84 self
.saveStdio
= (_sys
.stdout
, _sys
.stderr
)
86 self
.RedirectStdio(filename
)
88 # This finishes the initialization of wxWindows and then calls
89 # the OnInit that should be present in the derived class
95 self
.RestoreStdio() # Just in case the MainLoop was overridden
100 def SetTopWindow(self
, frame
):
102 self
.stdioWin
.SetParent(frame
)
103 wx
.PyApp
.SetTopWindow(self
, frame
)
107 wx
.PyApp
.MainLoop(self
)
111 def RedirectStdio(self
, filename
):
113 _sys
.stdout
= _sys
.stderr
= open(filename
, 'a')
115 self
.stdioWin
= self
.outputWindowClass()
116 _sys
.stdout
= _sys
.stderr
= self
.stdioWin
119 def RestoreStdio(self
):
120 _sys
.stdout
, _sys
.stderr
= self
.saveStdio
124 # change from wxPyApp_ to wxApp_
125 App_GetMacSupportPCMenuShortcuts
= _core
.PyApp_GetMacSupportPCMenuShortcuts
126 App_GetMacAboutMenuItemId
= _core
.PyApp_GetMacAboutMenuItemId
127 App_GetMacPreferencesMenuItemId
= _core
.PyApp_GetMacPreferencesMenuItemId
128 App_GetMacExitMenuItemId
= _core
.PyApp_GetMacExitMenuItemId
129 App_GetMacHelpMenuTitleName
= _core
.PyApp_GetMacHelpMenuTitleName
130 App_SetMacSupportPCMenuShortcuts
= _core
.PyApp_SetMacSupportPCMenuShortcuts
131 App_SetMacAboutMenuItemId
= _core
.PyApp_SetMacAboutMenuItemId
132 App_SetMacPreferencesMenuItemId
= _core
.PyApp_SetMacPreferencesMenuItemId
133 App_SetMacExitMenuItemId
= _core
.PyApp_SetMacExitMenuItemId
134 App_SetMacHelpMenuTitleName
= _core
.PyApp_SetMacHelpMenuTitleName
135 App_GetComCtl32Version
= _core
.PyApp_GetComCtl32Version
137 #----------------------------------------------------------------------------
139 class PySimpleApp(wx
.App
):
140 def __init__(self
, redirect
=False, filename
=None):
141 wx
.App
.__init
__(self
, redirect
, filename
)
143 wx
.InitAllImageHandlers()
147 class PyWidgetTester(wx
.App
):
148 def __init__(self
, size
= (250, 100)):
150 wx
.App
.__init
__(self
, 0)
153 self
.frame
= wxFrame(None, -1, "Widget Tester", pos
=(0,0), size
=self
.size
)
154 self
.SetTopWindow(self
.frame
)
157 def SetWidget(self
, widgetClass
, *args
):
158 w
= widgetClass(self
.frame
, *args
)
159 self
.frame
.Show(True)
161 #----------------------------------------------------------------------------
162 # DO NOT hold any other references to this object. This is how we
163 # know when to cleanup system resources that wxWin is holding. When
164 # the sys module is unloaded, the refcount on sys.__wxPythonCleanup
165 # goes to zero and it calls the wxApp_CleanUp function.
169 self
.cleanup
= _core
.App_CleanUp
173 _sys
.__wxPythonCleanup
= __wxPyCleanup()
175 ## # another possible solution, but it gets called too early...
176 ## if sys.version[0] == '2':
178 ## atexit.register(_core.wxApp_CleanUp)
180 ## sys.exitfunc = _core.wxApp_CleanUp
183 #----------------------------------------------------------------------------