5 import  wx
.lib
.infoframe
 
   7 #---------------------------------------------------------------------- 
   9 class MyFrame(wx
.Frame
): 
  10     def __init__(self
, output
): 
  11         wx
.Frame
.__init
__(self
, None, -1, "Close me...", size
=(300,100)) 
  13         menubar 
= wx
.MenuBar() 
  18         # Enable output menu item 
  20         menu
.Append(mID
, "&Enable output", "Display output frame") 
  21         self
.Bind(wx
.EVT_MENU
, output
.EnableOutput
, id=mID
) 
  23         # Disable output menu item 
  25         menu
.Append(mID
, "&Disable output", "Close output frame") 
  26         self
.Bind(wx
.EVT_MENU
, output
.DisableOutput
, id=mID
) 
  28         # Attach the menu to our menu bar 
  29         menubar
.Append(menu
, "&Output") 
  31         # Attach menu bar to frame 
  32         self
.SetMenuBar(menubar
) 
  34         # Point to ourselves as the output object's parent. 
  35         output
.SetParent(self
) 
  37         # Associate menu bar with output object 
  38         output
.SetOtherMenuBar(menubar
, menuname
="Output") 
  40         self
.Bind(wx
.EVT_CLOSE
, self
.OnClose
) 
  41         # We're going to set up a timer; set up an event handler for it. 
  42         self
.Bind(wx
.EVT_TIMER
, self
.OnTimer
) 
  44         # Set up a timer for demo purposes 
  45         self
.timer 
= wx
.Timer(self
, -1) 
  46         self
.timer
.Start(1000) 
  48         # Get a copy of stdout and set it aside. We'll use it later. 
  49         self
.save_stdout 
= sys
.stdout
 
  51         # Now point to the output object for stdout 
  52         sys
.stdout 
= self
.output 
= output
 
  56     def OnClose(self
,event
): 
  57         # We stored a pointer to the original stdout above in .__init__(), and 
  58         # here we restore it before closing the window. 
  59         sys
.stdout 
= self
.save_stdout
 
  68     # Event handler for timer events. 
  69     def OnTimer(self
, evt
): 
  70         print "This was printed with \"print\"" 
  73 #---------------------------------------------------------------------- 
  75 overview 
= wx
.lib
.infoframe
.__doc
__ 
  77 def runTest(frame
, nb
, log
): 
  79     This method is used by the wxPython Demo Framework for integrating 
  80     this demo with the rest. 
  82     win 
= MyFrame(wx
.lib
.infoframe
.PyInformationalMessagesFrame()) 
  86 #---------------------------------------------------------------------- 
  88 if __name__ 
== "__main__": 
  89 ##     class MyFrame(wxFrame): 
  90 ##         def __init__(self,output): 
  91 ##             wxFrame.__init__(self,None,-1,"Close me...",size=(300,100)) 
  92 ##             EVT_CLOSE(self,self.OnClose) 
  93 ##             menubar = wxMenuBar() 
  96 ##             menu.Append(mID,"&Enable output","Display output frame") 
  97 ##             EVT_MENU(self,mID,output.EnableOutput) 
  99 ##             menu.Append(mID,"&Disable output","Close output frame") 
 100 ##             EVT_MENU(self,mID,output.DisableOutput) 
 101 ##             menubar.Append(menu,"&Output") 
 102 ##             self.SetMenuBar(menubar) 
 103 ##             output.SetOtherMenuBar(menubar,menuname="Output") 
 105 ##         def OnClose(self,event): 
 106 ##             if isinstance(sys.stdout,wx.lib.infoframe.PyInformationalMessagesFrame): 
 107 ##                 sys.stdout.close() 
 112         # Override the default output window and point it to the 
 114         outputWindowClass 
= wx
.lib
.infoframe
.PyInformationalMessagesFrame
 
 118             # At this point, we should probably check to see if self.stdioWin 
 119             # is actually pointed to something. By default, wx.App() sets this 
 120             # attribute to None. This causes problems when setting up the menus 
 121             # in MyFrame() above. On the other hand, since there's little that 
 122             # can be done at this point, you might be better served putting 
 123             # an error handler directly into MyFrame(). 
 125             # That's in practice. In the case of this demo, the whole point 
 126             # of the exercise is to demonstrate the window, so we're being  
 127             # just a little lazy for clarity's sake. But do be careful in 
 128             # a 'real world' implementation :-) 
 130             frame 
= MyFrame(self
.stdioWin
) 
 132             self
.SetTopWindow(frame
) 
 134             # Associate the frame with stdout. 
 135             if isinstance(sys
.stdout
, wx
.lib
.infoframe
.PyInformationalMessagesFrame
): 
 136                 sys
.stdout
.SetParent(frame
) 
 141     # *extremely important* 
 143     # In this demo, if the redirect flag is set to False, the infoframe will not 
 144     # be created or used. All output will go to the default stdout, which in this 
 145     # case will cause the app to throw an exception. In a real app, you should 
 146     # probably plan ahead and add a check before forging ahead. See suggestion above.