]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
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 | |
d4b73b1b | 15 | import wx.lib.infoframe |
03a2c062 RD |
16 | |
17 | #---------------------------------------------------------------------- | |
18 | ||
8fa876ca RD |
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 | |
03a2c062 | 42 | self.SetMenuBar(menubar) |
8fa876ca RD |
43 | |
44 | # Point to ourselves as the output object's parent. | |
03a2c062 | 45 | output.SetParent(self) |
8fa876ca RD |
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) | |
03a2c062 | 53 | |
8fa876ca RD |
54 | # Set up a timer for demo purposes |
55 | self.timer = wx.Timer(self, -1) | |
03a2c062 RD |
56 | self.timer.Start(1000) |
57 | ||
8fa876ca | 58 | # Get a copy of stdout and set it aside. We'll use it later. |
03a2c062 | 59 | self.save_stdout = sys.stdout |
b881fc78 | 60 | |
8fa876ca | 61 | # Now point to the output object for stdout |
03a2c062 | 62 | sys.stdout = self.output = output |
8fa876ca | 63 | # ... and use it. |
03a2c062 RD |
64 | print "Hello!" |
65 | ||
66 | def OnClose(self,event): | |
8fa876ca RD |
67 | # We stored a pointer to the original stdout above in .__init__(), and |
68 | # here we restore it before closing the window. | |
03a2c062 | 69 | sys.stdout = self.save_stdout |
8fa876ca RD |
70 | |
71 | # Clean up | |
03a2c062 RD |
72 | self.output.close() |
73 | self.timer.Stop() | |
74 | self.timer = None | |
8fa876ca | 75 | |
03a2c062 RD |
76 | self.Destroy() |
77 | ||
8fa876ca | 78 | # Event handler for timer events. |
03a2c062 RD |
79 | def OnTimer(self, evt): |
80 | print "This was printed with \"print\"" | |
81 | ||
82 | ||
83 | #---------------------------------------------------------------------- | |
84 | ||
d4b73b1b | 85 | overview = wx.lib.infoframe.__doc__ |
03a2c062 RD |
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 | """ | |
d4b73b1b | 92 | win = MyFrame(wx.lib.infoframe.PyInformationalMessagesFrame()) |
03a2c062 RD |
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() | |
1fded56b | 105 | ## mID = wxNewId() |
03a2c062 RD |
106 | ## menu.Append(mID,"&Enable output","Display output frame") |
107 | ## EVT_MENU(self,mID,output.EnableOutput) | |
1fded56b | 108 | ## mID = wxNewId() |
03a2c062 RD |
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): | |
d4b73b1b | 116 | ## if isinstance(sys.stdout,wx.lib.infoframe.PyInformationalMessagesFrame): |
03a2c062 RD |
117 | ## sys.stdout.close() |
118 | ## self.Destroy() | |
119 | ||
b881fc78 RD |
120 | class MyApp(wx.App): |
121 | ||
8fa876ca RD |
122 | # Override the default output window and point it to the |
123 | # custom class. | |
d4b73b1b | 124 | outputWindowClass = wx.lib.infoframe.PyInformationalMessagesFrame |
8fa876ca | 125 | |
03a2c062 | 126 | def OnInit(self): |
8fa876ca RD |
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 | ||
03a2c062 | 140 | frame = MyFrame(self.stdioWin) |
1e4a197e | 141 | frame.Show(True) |
03a2c062 | 142 | self.SetTopWindow(frame) |
8fa876ca RD |
143 | |
144 | # Associate the frame with stdout. | |
d4b73b1b | 145 | if isinstance(sys.stdout, wx.lib.infoframe.PyInformationalMessagesFrame): |
03a2c062 | 146 | sys.stdout.SetParent(frame) |
8fa876ca | 147 | |
03a2c062 | 148 | print "Starting.\n", |
1e4a197e | 149 | return True |
03a2c062 | 150 | |
8fa876ca RD |
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) | |
03a2c062 | 158 | app.MainLoop() |