| 1 | |
| 2 | import sys |
| 3 | |
| 4 | import wx |
| 5 | import wx.lib.infoframe |
| 6 | |
| 7 | #---------------------------------------------------------------------- |
| 8 | |
| 9 | class MyFrame(wx.Frame): |
| 10 | def __init__(self, output): |
| 11 | wx.Frame.__init__(self, None, -1, "Close me...", size=(300,100)) |
| 12 | |
| 13 | menubar = wx.MenuBar() |
| 14 | |
| 15 | # Output menu |
| 16 | menu = wx.Menu() |
| 17 | |
| 18 | # Enable output menu item |
| 19 | mID = wx.NewId() |
| 20 | menu.Append(mID, "&Enable output", "Display output frame") |
| 21 | self.Bind(wx.EVT_MENU, output.EnableOutput, id=mID) |
| 22 | |
| 23 | # Disable output menu item |
| 24 | mID = wx.NewId() |
| 25 | menu.Append(mID, "&Disable output", "Close output frame") |
| 26 | self.Bind(wx.EVT_MENU, output.DisableOutput, id=mID) |
| 27 | |
| 28 | # Attach the menu to our menu bar |
| 29 | menubar.Append(menu, "&Output") |
| 30 | |
| 31 | # Attach menu bar to frame |
| 32 | self.SetMenuBar(menubar) |
| 33 | |
| 34 | # Point to ourselves as the output object's parent. |
| 35 | output.SetParent(self) |
| 36 | |
| 37 | # Associate menu bar with output object |
| 38 | output.SetOtherMenuBar(menubar, menuname="Output") |
| 39 | |
| 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) |
| 43 | |
| 44 | # Set up a timer for demo purposes |
| 45 | self.timer = wx.Timer(self, -1) |
| 46 | self.timer.Start(1000) |
| 47 | |
| 48 | # Get a copy of stdout and set it aside. We'll use it later. |
| 49 | self.save_stdout = sys.stdout |
| 50 | |
| 51 | # Now point to the output object for stdout |
| 52 | sys.stdout = self.output = output |
| 53 | # ... and use it. |
| 54 | print "Hello!" |
| 55 | |
| 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 |
| 60 | |
| 61 | # Clean up |
| 62 | self.output.close() |
| 63 | self.timer.Stop() |
| 64 | self.timer = None |
| 65 | |
| 66 | self.Destroy() |
| 67 | |
| 68 | # Event handler for timer events. |
| 69 | def OnTimer(self, evt): |
| 70 | print "This was printed with \"print\"" |
| 71 | |
| 72 | |
| 73 | #---------------------------------------------------------------------- |
| 74 | |
| 75 | overview = wx.lib.infoframe.__doc__ |
| 76 | |
| 77 | def runTest(frame, nb, log): |
| 78 | """ |
| 79 | This method is used by the wxPython Demo Framework for integrating |
| 80 | this demo with the rest. |
| 81 | """ |
| 82 | win = MyFrame(wx.lib.infoframe.PyInformationalMessagesFrame()) |
| 83 | frame.otherWin = win |
| 84 | win.Show(1) |
| 85 | |
| 86 | #---------------------------------------------------------------------- |
| 87 | |
| 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() |
| 94 | ## menu = wxMenu() |
| 95 | ## mID = wxNewId() |
| 96 | ## menu.Append(mID,"&Enable output","Display output frame") |
| 97 | ## EVT_MENU(self,mID,output.EnableOutput) |
| 98 | ## mID = wxNewId() |
| 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") |
| 104 | |
| 105 | ## def OnClose(self,event): |
| 106 | ## if isinstance(sys.stdout,wx.lib.infoframe.PyInformationalMessagesFrame): |
| 107 | ## sys.stdout.close() |
| 108 | ## self.Destroy() |
| 109 | |
| 110 | class MyApp(wx.App): |
| 111 | |
| 112 | # Override the default output window and point it to the |
| 113 | # custom class. |
| 114 | outputWindowClass = wx.lib.infoframe.PyInformationalMessagesFrame |
| 115 | |
| 116 | def OnInit(self): |
| 117 | |
| 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(). |
| 124 | # |
| 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 :-) |
| 129 | |
| 130 | frame = MyFrame(self.stdioWin) |
| 131 | frame.Show(True) |
| 132 | self.SetTopWindow(frame) |
| 133 | |
| 134 | # Associate the frame with stdout. |
| 135 | if isinstance(sys.stdout, wx.lib.infoframe.PyInformationalMessagesFrame): |
| 136 | sys.stdout.SetParent(frame) |
| 137 | |
| 138 | print "Starting.\n", |
| 139 | return True |
| 140 | |
| 141 | # *extremely important* |
| 142 | # |
| 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. |
| 147 | app = MyApp(True) |
| 148 | app.MainLoop() |