Allow the stdout redirection to be toggled on or off in the demo

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26175 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn 2004-03-11 06:26:54 +00:00
parent dd346b947b
commit 330af86940
2 changed files with 23 additions and 6 deletions

View File

@ -493,9 +493,15 @@ class wxPythonDemo(wx.Frame):
# Make a File menu
self.mainmenu = wx.MenuBar()
menu = wx.Menu()
item = menu.Append(-1, '&Redirect Output',
'Redirect print statements to a window',
wx.ITEM_CHECK)
self.Bind(wx.EVT_MENU, self.OnToggleRedirect, item)
item = menu.Append(-1, 'E&xit\tAlt-X', 'Get the heck outta here!')
self.Bind(wx.EVT_MENU, self.OnFileExit, item)
wx.App_SetMacExitMenuItemId(item.GetId())
self.mainmenu.Append(menu, '&File')
# Make a Demo menu
@ -743,6 +749,15 @@ class wxPythonDemo(wx.Frame):
def OnFileExit(self, *event):
self.Close()
def OnToggleRedirect(self, event):
app = wx.GetApp()
if event.Checked():
app.RedirectStdio()
print "Print statements and other standard output will now be directed to this window."
else:
app.RestoreStdio()
print "Print statements and other standard output will now be sent to the usual location."
def OnHelpAbout(self, event):
from About import MyAboutBox
about = MyAboutBox(self)

View File

@ -29,6 +29,13 @@ class PyOnDemandOutputWindow:
EVT_CLOSE(self.frame, self.OnCloseWindow)
def OnCloseWindow(self, event):
if self.frame is not None:
self.frame.Destroy()
self.frame = None
self.text = None
# These methods provide the file-like output behaviour.
def write(self, text):
"""
@ -53,11 +60,6 @@ class PyOnDemandOutputWindow:
wx.CallAfter(self.frame.Close)
def OnCloseWindow(self, event):
if self.frame is not None:
self.frame.Destroy()
self.frame = None
self.text = None
#----------------------------------------------------------------------
@ -131,7 +133,7 @@ your Mac."""
self.RestoreStdio()
def RedirectStdio(self, filename):
def RedirectStdio(self, filename=None):
"""Redirect sys.stdout and sys.stderr to a file or a popup window."""
if filename:
_sys.stdout = _sys.stderr = open(filename, 'a')