]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/infoframe.py
doc tweaks, typo fixed, etc.
[wxWidgets.git] / wxPython / demo / infoframe.py
1 # 11/12/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 # o wx module doesn't like the doc string.
5 #
6 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
7 #
8 # o Library didn't get hit by the wx renamer.
9 # o Docstring issues resolved.
10 #
11
12 import sys
13
14 import wx
15 import wx.lib.infoframe
16
17 #----------------------------------------------------------------------
18
19 class MyFrame(wx.Frame):
20 def __init__(self, output):
21 wx.Frame.__init__(self, None, -1, "Close me...", size=(300,100))
22
23 menubar = wx.MenuBar()
24
25 # Output menu
26 menu = wx.Menu()
27
28 # Enable output menu item
29 mID = wx.NewId()
30 menu.Append(mID, "&Enable output", "Display output frame")
31 self.Bind(wx.EVT_MENU, output.EnableOutput, id=mID)
32
33 # Disable output menu item
34 mID = wx.NewId()
35 menu.Append(mID, "&Disable output", "Close output frame")
36 self.Bind(wx.EVT_MENU, output.DisableOutput, id=mID)
37
38 # Attach the menu to our menu bar
39 menubar.Append(menu, "&Output")
40
41 # Attach menu bar to frame
42 self.SetMenuBar(menubar)
43
44 # Point to ourselves as the output object's parent.
45 output.SetParent(self)
46
47 # Associate menu bar with output object
48 output.SetOtherMenuBar(menubar, menuname="Output")
49
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)
53
54 # Set up a timer for demo purposes
55 self.timer = wx.Timer(self, -1)
56 self.timer.Start(1000)
57
58 # Get a copy of stdout and set it aside. We'll use it later.
59 self.save_stdout = sys.stdout
60
61 # Now point to the output object for stdout
62 sys.stdout = self.output = output
63 # ... and use it.
64 print "Hello!"
65
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
70
71 # Clean up
72 self.output.close()
73 self.timer.Stop()
74 self.timer = None
75
76 self.Destroy()
77
78 # Event handler for timer events.
79 def OnTimer(self, evt):
80 print "This was printed with \"print\""
81
82
83 #----------------------------------------------------------------------
84
85 overview = wx.lib.infoframe.__doc__
86
87 def runTest(frame, nb, log):
88 """
89 This method is used by the wxPython Demo Framework for integrating
90 this demo with the rest.
91 """
92 win = MyFrame(wx.lib.infoframe.PyInformationalMessagesFrame())
93 frame.otherWin = win
94 win.Show(1)
95
96 #----------------------------------------------------------------------
97
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()
104 ## menu = wxMenu()
105 ## mID = wxNewId()
106 ## menu.Append(mID,"&Enable output","Display output frame")
107 ## EVT_MENU(self,mID,output.EnableOutput)
108 ## mID = wxNewId()
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")
114
115 ## def OnClose(self,event):
116 ## if isinstance(sys.stdout,wx.lib.infoframe.PyInformationalMessagesFrame):
117 ## sys.stdout.close()
118 ## self.Destroy()
119
120 class MyApp(wx.App):
121
122 # Override the default output window and point it to the
123 # custom class.
124 outputWindowClass = wx.lib.infoframe.PyInformationalMessagesFrame
125
126 def OnInit(self):
127
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().
134 #
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 :-)
139
140 frame = MyFrame(self.stdioWin)
141 frame.Show(True)
142 self.SetTopWindow(frame)
143
144 # Associate the frame with stdout.
145 if isinstance(sys.stdout, wx.lib.infoframe.PyInformationalMessagesFrame):
146 sys.stdout.SetParent(frame)
147
148 print "Starting.\n",
149 return True
150
151 # *extremely important*
152 #
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.
157 app = MyApp(True)
158 app.MainLoop()