1 # 11/12/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
4 # o wx module doesn't like the doc string.
6 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
8 # o Library didn't get hit by the wx renamer.
9 # o Docstring issues resolved.
15 import wx
.lib
.infoframe
17 #----------------------------------------------------------------------
19 class MyFrame(wx
.Frame
):
20 def __init__(self
, output
):
21 wx
.Frame
.__init
__(self
, None, -1, "Close me...", size
=(300,100))
23 menubar
= wx
.MenuBar()
28 # Enable output menu item
30 menu
.Append(mID
, "&Enable output", "Display output frame")
31 self
.Bind(wx
.EVT_MENU
, output
.EnableOutput
, id=mID
)
33 # Disable output menu item
35 menu
.Append(mID
, "&Disable output", "Close output frame")
36 self
.Bind(wx
.EVT_MENU
, output
.DisableOutput
, id=mID
)
38 # Attach the menu to our menu bar
39 menubar
.Append(menu
, "&Output")
41 # Attach menu bar to frame
42 self
.SetMenuBar(menubar
)
44 # Point to ourselves as the output object's parent.
45 output
.SetParent(self
)
47 # Associate menu bar with output object
48 output
.SetOtherMenuBar(menubar
, menuname
="Output")
50 self
.Bind(wx
.EVT_CLOSE
, self
.OnClose
)
51 # We're going to set up a timer; set up an event handler for it.
52 self
.Bind(wx
.EVT_TIMER
, self
.OnTimer
)
54 # Set up a timer for demo purposes
55 self
.timer
= wx
.Timer(self
, -1)
56 self
.timer
.Start(1000)
58 # Get a copy of stdout and set it aside. We'll use it later.
59 self
.save_stdout
= sys
.stdout
61 # Now point to the output object for stdout
62 sys
.stdout
= self
.output
= output
66 def OnClose(self
,event
):
67 # We stored a pointer to the original stdout above in .__init__(), and
68 # here we restore it before closing the window.
69 sys
.stdout
= self
.save_stdout
78 # Event handler for timer events.
79 def OnTimer(self
, evt
):
80 print "This was printed with \"print\""
83 #----------------------------------------------------------------------
85 overview
= wx
.lib
.infoframe
.__doc
__
87 def runTest(frame
, nb
, log
):
89 This method is used by the wxPython Demo Framework for integrating
90 this demo with the rest.
92 win
= MyFrame(wx
.lib
.infoframe
.PyInformationalMessagesFrame())
96 #----------------------------------------------------------------------
98 if __name__
== "__main__":
99 ## class MyFrame(wxFrame):
100 ## def __init__(self,output):
101 ## wxFrame.__init__(self,None,-1,"Close me...",size=(300,100))
102 ## EVT_CLOSE(self,self.OnClose)
103 ## menubar = wxMenuBar()
106 ## menu.Append(mID,"&Enable output","Display output frame")
107 ## EVT_MENU(self,mID,output.EnableOutput)
109 ## menu.Append(mID,"&Disable output","Close output frame")
110 ## EVT_MENU(self,mID,output.DisableOutput)
111 ## menubar.Append(menu,"&Output")
112 ## self.SetMenuBar(menubar)
113 ## output.SetOtherMenuBar(menubar,menuname="Output")
115 ## def OnClose(self,event):
116 ## if isinstance(sys.stdout,wx.lib.infoframe.PyInformationalMessagesFrame):
117 ## sys.stdout.close()
122 # Override the default output window and point it to the
124 outputWindowClass
= wx
.lib
.infoframe
.PyInformationalMessagesFrame
128 # At this point, we should probably check to see if self.stdioWin
129 # is actually pointed to something. By default, wx.App() sets this
130 # attribute to None. This causes problems when setting up the menus
131 # in MyFrame() above. On the other hand, since there's little that
132 # can be done at this point, you might be better served putting
133 # an error handler directly into MyFrame().
135 # That's in practice. In the case of this demo, the whole point
136 # of the exercise is to demonstrate the window, so we're being
137 # just a little lazy for clarity's sake. But do be careful in
138 # a 'real world' implementation :-)
140 frame
= MyFrame(self
.stdioWin
)
142 self
.SetTopWindow(frame
)
144 # Associate the frame with stdout.
145 if isinstance(sys
.stdout
, wx
.lib
.infoframe
.PyInformationalMessagesFrame
):
146 sys
.stdout
.SetParent(frame
)
151 # *extremely important*
153 # In this demo, if the redirect flag is set to False, the infoframe will not
154 # be created or used. All output will go to the default stdout, which in this
155 # case will cause the app to throw an exception. In a real app, you should
156 # probably plan ahead and add a check before forging ahead. See suggestion above.