1 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o wxPyInformationalMessagesFrame -> PyInformationalMessagesFrame
4 # o dummy_wxPyInformationalMessagesFrame -> dummy_PyInformationalMessagesFrame
9 Released under wxWindows license etc.
11 This is a fairly rudimentary, but slightly fancier tha
12 wxPyOnDemandOutputWindow (on which it's based; thanks Robin), version
13 of the same sort of thing: a file-like class called
14 wxInformationalMessagesFrame. This window also has a status bar with a
15 couple of buttons for controlling the echoing of all output to a file
16 with a randomly-chosen filename...
18 The class behaves similarly to wxPyOnDemandOutputWindow in that (at
19 least by default) the frame does not appear until written to, but is
20 somewhat different in that, either under programmatic (the
21 DisableOutput method) or user (the frame's close button, it's status
22 bar's "Dismiss" button, or a "Disable output" item of some menu,
23 perhaps of some other frame), the frame will be destroyed, an
24 associated file closed, and writing to it will then do nothing. This
25 can be reversed: either under programmatic (the EnableOutput method)
26 or user (an "Enable output" item of some menu), a new frame will be
27 opened,And an associated file (with a "randomly"selected filename)
28 opened for writing [to which all subsequent displayed messages will be
31 Please note that, like wxPyOnDemandOutputWindow, the instance is not
32 itself a subclass of wxWindow: when the window is open (and ONLY
33 then), it's "frame" attribute is the actual instance of wFrame...
37 from wxPython.lib.infoframe import *
38 ... # ... modify your wxApp as follows:
40 outputWindowClass = PyInformationalMessagesFrame
43 If you're running on Linux, you'll also have to supply an argument 1 to your
44 constructor of myApp to redirect stdout/stderr to this window (it's done
45 automatically for you on Windows).
47 If you don't want to redirect stdout/stderr, but use the class directly: do
50 InformationalMessagesFrame = PyInformationalMessagesFrame\
51 ([options from progname (default ""),
52 txt (default "informational messages"])
54 #^^^^ early in the program
56 InformationalMessagesFrame([comma-separated list of items to
57 display. Note that these will never
58 be separated by spaces as they may
59 be when used in the Python 'print'
62 The latter statement, of course, may be repeated arbitrarily often.
63 The window will not appear until it is written to, and it may be
64 manually closed by the user, after which it will reappear again until
65 written to... Also note that all output is echoed to a file with a
66 randomly-generated name [see the mktemp module in the standard
67 library], in the directory given as the 'dir' keyword argument to the
68 InformationalMessagesFrame constructor [which has a default value of
69 '.'), or set via the method SetOutputDirectory... This file will be
70 closed with the window--a new one will be created [by default] upon
71 each subsequent reopening.
73 Please also note the methods EnableOutput and DisableOutput, and the
74 possible arguments for the constructor in the code below... (* TO DO:
75 explain this here...*) Neither of these methods need be used at all,
76 and in this case the frame will only be displayed once it has been
77 written to, like wxPyOnDemandOutputWindow.
79 The former, EnableOutput, displays the frame with an introductory
80 message, opens a random file to which future displayed output also
81 goes (unless the nofile attribute is present), and sets the __debug__
82 variable of each module to 1 (unless the no __debug__ attribute is
83 present]. This is so that you can say, in any module whatsoever,
86 InformationalMessagesFrame("... with lots of %<Character> constructs"
89 without worrying about the overhead of evaluating the arguments, and
90 calling the wxInformationalMessagesFrame instance, in the case where
91 debugging is not turned on. (This won't happen if the instance has an
92 attribute no__debug__; you can arrange this by sub-classing...)
94 "Debug mode" can also be turned on by selecting the item-"Enable
95 output" from the "Debug" menu [the default--see the optional arguments
96 to the SetOtherMenuBar method] of a frame which has been either passed
97 appropriately to the constructor of the wxInformationalMessagesFrame
98 (see the code), or set via the SetOtherMenuBar method thereof. This
99 also writes an empty string to the instance, meaning that the frame
100 will open (unless DisablOutput has been called) with an appropriate
101 introductory message (which will vary according to whether the
102 instance/class has the "no __debug__" attribute)^ I have found this to
103 be an extremely useful tool, in lieu of a full wxPython debugger...
105 Following this, the menu item is also disabled, and an item "Disable
106 output" (again, by default) is enabled. Note that these need not be
107 done: e.g., you don't NEED to have a menu with appropriate items; in
108 this case simply do not call the SetOtherMenuBar method or use the
109 othermenubar keyword argument of the class instance constructor.
111 The DisableOutput method does the reverse of this; it closes the
112 window (and associated file), and sets the __debug__ variable of each
113 module whose name begins with a capital letter {this happens to be the
114 author's personal practice; all my python module start with capital
115 letters} to 0. It also enables/disabled the appropriate menu items,
116 if this was done previously (or SetOtherMenuBar has been called...).
117 Note too that after a call to DisableOutput, nothing further will be
118 done upon subsequent write()'s, until the EnableOutput method is
119 called, either explicitly or by the menu selection above...
121 Finally, note that the file-like method close() destroys the window
122 (and closes any associated file) and there is a file-like method
123 write() which displays it's argument.
125 All (well, most) of this is made clear by the example code at the end
126 of this file, which is run if the file is run by itself; otherwise,
127 see the appropriate "stub" file in the wxPython demo.
137 class _MyStatusBar(wx
.StatusBar
):
138 def __init__(self
, parent
, callbacks
=None, useopenbutton
=0):
139 wx
.StatusBar
.__init
__(self
, parent
, -1, style
=wx
.TAB_TRAVERSAL
)
140 self
.SetFieldsCount(3)
142 self
.SetStatusText("",0)
144 self
.button1
= wx
.Button(self
, -1, "Dismiss", style
=wx
.TAB_TRAVERSAL
)
145 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton1
, self
.button1
)
147 if not useopenbutton
:
148 self
.button2
= wx
.Button(self
, -1, "Close File", style
=wx
.TAB_TRAVERSAL
)
150 self
.button2
= wx
.Button(self
, -1, "Open New File", style
=wx
.TAB_TRAVERSAL
)
152 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton2
, self
.button2
)
153 self
.useopenbutton
= useopenbutton
154 self
.callbacks
= callbacks
156 # figure out how tall to make the status bar
157 dc
= wx
.ClientDC(self
)
158 dc
.SetFont(self
.GetFont())
159 (w
,h
) = dc
.GetTextExtent('X')
161 self
.SetSize((100, h
))
163 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
165 # reposition things...
166 def OnSize(self
, event
):
167 self
.CalculateSizes()
168 rect
= self
.GetFieldRect(1)
169 self
.button1
.SetPosition((rect
.x
+5, rect
.y
+2))
170 self
.button1
.SetSize((rect
.width
-10, rect
.height
-4))
171 rect
= self
.GetFieldRect(2)
172 self
.button2
.SetPosition((rect
.x
+5, rect
.y
+2))
173 self
.button2
.SetSize((rect
.width
-10, rect
.height
-4))
176 def CalculateSizes(self
):
177 dc
= wx
.ClientDC(self
.button1
)
178 dc
.SetFont(self
.button1
.GetFont())
179 (w1
,h
) = dc
.GetTextExtent(self
.button1
.GetLabel())
181 dc
= wx
.ClientDC(self
.button2
)
182 dc
.SetFont(self
.button2
.GetFont())
183 (w2
,h
) = dc
.GetTextExtent(self
.button2
.GetLabel())
185 self
.SetStatusWidths([-1,w1
+15,w2
+15])
187 def OnButton1(self
,event
):
190 def OnButton2(self
,event
):
191 if self
.useopenbutton
and self
.callbacks
[2] ():
192 self
.button2
.SetLabel ("Close File")
193 elif self
.callbacks
[1] ():
194 self
.button2
.SetLabel ("Open New File")
196 self
.useopenbutton
= 1 - self
.useopenbutton
198 self
.button2
.Refresh(True)
203 class PyInformationalMessagesFrame
:
206 text
="informational messages",
209 enableitem
="Enable output",
210 disableitem
="Disable output",
213 self
.SetOtherMenuBar(othermenubar
,
215 enableitem
=enableitem
,
216 disableitem
=disableitem
)
218 if hasattr(self
,"othermenu") and self
.othermenu
is not None:
219 i
= self
.othermenu
.FindMenuItem(self
.menuname
,self
.disableitem
)
220 self
.othermenu
.Enable(i
,0)
221 i
= self
.othermenu
.FindMenuItem(self
.menuname
,self
.enableitem
)
222 self
.othermenu
.Enable(i
,1)
225 self
.title
= "%s %s" % (progname
,text
)
226 self
.parent
= None # use the SetParent method if desired...
227 self
.softspace
= 1 # of rather limited use
230 self
.SetOutputDirectory(dir)
233 def SetParent(self
, parent
):
237 def SetOtherMenuBar(self
,
240 enableitem
="Enable output",
241 disableitem
="Disable output"):
242 self
.othermenu
= othermenu
243 self
.menuname
= menuname
244 self
.enableitem
= enableitem
245 self
.disableitem
= disableitem
251 def write(self
, string
):
252 if not wx
.Thread_IsMain():
253 # Aquire the GUI mutex before making GUI calls. Mutex is released
254 # when locker is deleted at the end of this function.
256 # TODO: This should be updated to use wx.CallAfter similarly to how
257 # PyOnDemandOutputWindow.write was so it is not necessary
258 # to get the gui mutex
259 locker
= wx
.MutexGuiLocker()
267 if (hasattr(self
,"text")
268 and self
.text
is not None
269 and self
.text
.GetInsertionPoint() != self
.text
.GetLastPosition()):
273 self
.frame
= wx
.Frame(self
.parent
, -1, self
.title
, size
=(450, 300),
274 style
=wx
.DEFAULT_FRAME_STYLE|wx
.NO_FULL_REPAINT_ON_RESIZE
)
276 self
.text
= wx
.TextCtrl(self
.frame
, -1, "",
277 style
= wx
.TE_MULTILINE|wx
.TE_READONLY|wx
.TE_RICH
)
279 self
.frame
.sb
= _MyStatusBar(self
.frame
,
280 callbacks
=[self
.DisableOutput
,
283 useopenbutton
=hasattr(self
,
285 self
.frame
.SetStatusBar(self
.frame
.sb
)
286 self
.frame
.Show(True)
287 self
.frame
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
)
289 if hasattr(self
,"nofile"):
290 self
.text
.AppendText(
291 "Please close this window (or select the "
292 "'Dismiss' button below) when desired. By "
293 "default all messages written to this window "
294 "will NOT be written to a file--you "
295 "may change this by selecting 'Open New File' "
296 "below, allowing you to select a "
299 tempfile
.tempdir
= self
.dir
300 filename
= os
.path
.abspath(tempfile
.mktemp ())
302 self
.text
.AppendText(
303 "Please close this window (or select the "
304 "'Dismiss' button below) when desired. By "
305 "default all messages written to this window "
306 "will also be written to the file '%s'--you "
307 "may close this file by selecting 'Close "
308 "File' below, whereupon this button will be "
309 "replaced with one allowing you to select a "
310 "new file...\n\n" % filename
)
312 self
.f
= open(filename
, 'w')
313 self
.frame
.sb
.SetStatusText("File '%s' opened..."
316 except EnvironmentError:
317 self
.frame
.sb
.SetStatusText("File creation failed "
321 self
.text
.AppendText(string
)
324 self
.text
.ShowPosition(self
.text
.GetLastPosition())
326 if not hasattr(self
,"no__debug__"):
327 for m
in sys
.modules
.values():
328 if m
is not None:# and m.__dict__.has_key("__debug__"):
329 m
.__dict
__["__debug__"] = 1
331 if hasattr(self
,"othermenu") and self
.othermenu
is not None:
332 i
= self
.othermenu
.FindMenuItem(self
.menuname
,self
.disableitem
)
333 self
.othermenu
.Enable(i
,1)
334 i
= self
.othermenu
.FindMenuItem(self
.menuname
,self
.enableitem
)
335 self
.othermenu
.Enable(i
,0)
340 def OnCloseWindow(self
, event
, exiting
=0):
345 if (hasattr(self
,"othermenu") and self
.othermenu
is not None
346 and self
.frame
is not None
349 i
= self
.othermenu
.FindMenuItem(self
.menuname
,self
.disableitem
)
350 self
.othermenu
.Enable(i
,0)
351 i
= self
.othermenu
.FindMenuItem(self
.menuname
,self
.enableitem
)
352 self
.othermenu
.Enable(i
,1)
354 if not hasattr(self
,"no__debug__"):
355 for m
in sys
.modules
.values():
356 if m
is not None:# and m.__dict__.has_key("__debug__"):
357 m
.__dict
__["__debug__"] = 0
359 if self
.frame
is not None: # typically True, but, e.g., allows
360 # DisableOutput method (which calls this
361 # one) to be called when the frame is not
362 # actually open, so that it is always safe
363 # to call this method...
365 self
.frame
= self
.text
= None
370 def EnableOutput(self
,
371 event
=None,# event must be the first optional argument...
374 enableitem
="Enable output",
375 disableitem
="Disable output"):
377 if othermenubar
is not None:
378 self
.SetOtherMenuBar(othermenubar
,
380 enableitem
=enableitem
,
381 disableitem
=disableitem
)
392 self
.frame
.sb
.SetStatusText("File '%s' closed..."
393 % os
.path
.abspath(self
.f
.name
),
399 self
.frame
.sb
.SetStatusText("")
401 self
.frame
.sb
.Refresh()
405 def OpenNewFile(self
):
407 dlg
= wx
.FileDialog(self
.frame
,
408 "Choose a new log file", self
.dir,"","*",
409 wx
.SAVE | wx
.OVERWRITE_PROMPT
)
410 if dlg
.ShowModal() == wx
.ID_CANCEL
:
415 self
.f
= open(os
.path
.abspath(dlg
.GetPath()),'w')
416 except EnvironmentError:
421 self
.frame
.sb
.SetStatusText("File '%s' opened..."
422 % os
.path
.abspath(self
.f
.name
),
424 if hasattr(self
,"nofile"):
425 self
.frame
.sb
= _MyStatusBar(self
.frame
,
426 callbacks
=[self
.DisableOutput
,
429 self
.frame
.SetStatusBar(self
.frame
.sb
)
430 if hasattr(self
,"nofile"):
431 delattr(self
,"nofile")
435 def DisableOutput(self
,
436 event
=None,# event must be the first optional argument...
438 self
.write("<InformationalMessagesFrame>.DisableOutput()\n")
439 if hasattr(self
,"frame") \
440 and self
.frame
is not None:
441 self
.OnCloseWindow("Dummy",exiting
=exiting
)
451 self
.text
.SetInsertionPointEnd()
455 def __call__(self
,* args
):
460 def SetOutputDirectory(self
,dir):
461 self
.dir = os
.path
.abspath(dir)
462 ## sys.__stderr__.write("Directory: os.path.abspath(%s) = %s\n"
467 class Dummy_PyInformationalMessagesFrame
:
468 def __init__(self
,progname
=""):
470 def __call__(self
,*args
):
478 def EnableOutput(self
):
480 def __call__(self
,* args
):
482 def DisableOutput(self
,exiting
=0):
484 def SetParent(self
,wX
):