]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | |
2 | import sys | |
3 | ||
4 | import wx | |
d4b73b1b | 5 | import wx.lib.infoframe |
03a2c062 RD |
6 | |
7 | #---------------------------------------------------------------------- | |
8 | ||
8fa876ca RD |
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 | |
03a2c062 | 32 | self.SetMenuBar(menubar) |
8fa876ca RD |
33 | |
34 | # Point to ourselves as the output object's parent. | |
03a2c062 | 35 | output.SetParent(self) |
8fa876ca RD |
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) | |
03a2c062 | 43 | |
8fa876ca RD |
44 | # Set up a timer for demo purposes |
45 | self.timer = wx.Timer(self, -1) | |
03a2c062 RD |
46 | self.timer.Start(1000) |
47 | ||
8fa876ca | 48 | # Get a copy of stdout and set it aside. We'll use it later. |
03a2c062 | 49 | self.save_stdout = sys.stdout |
b881fc78 | 50 | |
8fa876ca | 51 | # Now point to the output object for stdout |
03a2c062 | 52 | sys.stdout = self.output = output |
8fa876ca | 53 | # ... and use it. |
03a2c062 RD |
54 | print "Hello!" |
55 | ||
56 | def OnClose(self,event): | |
8fa876ca RD |
57 | # We stored a pointer to the original stdout above in .__init__(), and |
58 | # here we restore it before closing the window. | |
03a2c062 | 59 | sys.stdout = self.save_stdout |
8fa876ca RD |
60 | |
61 | # Clean up | |
03a2c062 RD |
62 | self.output.close() |
63 | self.timer.Stop() | |
64 | self.timer = None | |
8fa876ca | 65 | |
03a2c062 RD |
66 | self.Destroy() |
67 | ||
8fa876ca | 68 | # Event handler for timer events. |
03a2c062 RD |
69 | def OnTimer(self, evt): |
70 | print "This was printed with \"print\"" | |
71 | ||
72 | ||
73 | #---------------------------------------------------------------------- | |
74 | ||
d4b73b1b | 75 | overview = wx.lib.infoframe.__doc__ |
03a2c062 RD |
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 | """ | |
d4b73b1b | 82 | win = MyFrame(wx.lib.infoframe.PyInformationalMessagesFrame()) |
03a2c062 RD |
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() | |
1fded56b | 95 | ## mID = wxNewId() |
03a2c062 RD |
96 | ## menu.Append(mID,"&Enable output","Display output frame") |
97 | ## EVT_MENU(self,mID,output.EnableOutput) | |
1fded56b | 98 | ## mID = wxNewId() |
03a2c062 RD |
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): | |
d4b73b1b | 106 | ## if isinstance(sys.stdout,wx.lib.infoframe.PyInformationalMessagesFrame): |
03a2c062 RD |
107 | ## sys.stdout.close() |
108 | ## self.Destroy() | |
109 | ||
b881fc78 RD |
110 | class MyApp(wx.App): |
111 | ||
8fa876ca RD |
112 | # Override the default output window and point it to the |
113 | # custom class. | |
d4b73b1b | 114 | outputWindowClass = wx.lib.infoframe.PyInformationalMessagesFrame |
8fa876ca | 115 | |
03a2c062 | 116 | def OnInit(self): |
8fa876ca RD |
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 | ||
03a2c062 | 130 | frame = MyFrame(self.stdioWin) |
1e4a197e | 131 | frame.Show(True) |
03a2c062 | 132 | self.SetTopWindow(frame) |
8fa876ca RD |
133 | |
134 | # Associate the frame with stdout. | |
d4b73b1b | 135 | if isinstance(sys.stdout, wx.lib.infoframe.PyInformationalMessagesFrame): |
03a2c062 | 136 | sys.stdout.SetParent(frame) |
8fa876ca | 137 | |
03a2c062 | 138 | print "Starting.\n", |
1e4a197e | 139 | return True |
03a2c062 | 140 | |
8fa876ca RD |
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) | |
03a2c062 | 148 | app.MainLoop() |