wxpython log redirect
kyosohma at gmail.com
kyosohma at gmail.com
Fri Aug 17 17:12:10 EDT 2007
On Aug 16, 2:29 pm, vedrandeko... at v-programs.com wrote:
> Hello,
>
> Why this wx example don't return \nHELLO WORLD and other text in same
> window:
>
> import wx
> import logging
> import sys
>
> def nekaj():
> print "\nHELLO WORLD"
>
> class WxLog(logging.Handler):
> def __init__(self, ctrl):
> logging.Handler.__init__(self)
> self.ctrl = ctrl
> def emit(self, record):
> self.ctrl.AppendText(self.format(record)+"\n")
>
> class MainFrame(wx.Frame):
> def __init__(self):
> wx.Frame.__init__(self, None, title="logging test")
> sizer = wx.BoxSizer(wx.VERTICAL)
>
> log = wx.TextCtrl(self, style=wx.TE_MULTILINE)
>
> rootLogger = logging.getLogger('')
> rootLogger.setLevel(logging.DEBUG)
> hdlr = WxLog(log)
> hdlr.setFormatter(logging.Formatter('%(levelname)s | %(name)s |
> %(message)s [@ %(asctime)s in %(filename)s:%(lineno)d]'))
> rootLogger.addHandler(hdlr)
> rootLogger.debug(str(sys.stdout))
> nekaj() # goes to the function nekaj
>
> if __name__ =="__main__":
> app = wx.App(0)
> frame = MainFrame()
> frame.Show()
> app.MainLoop()
>
> Regards,
> Vedran
Why are you using the logging module? All you need to do is redirect
stdout. See below:
Give this a try:
<code>
class XPinst(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
def OnInit(self):
self.frame = wx.Frame(None, -1, title='Redirect Test',
size=(620,450),
style=wx.STAY_ON_TOP|
wx.DEFAULT_FRAME_STYLE)
panel = wx.Panel(self.frame, -1)
self.log = wx.TextCtrl(panel, -1, size=(500,400),
style = wx.TE_MULTILINE|wx.TE_READONLY|
wx.HSCROLL)
redir=RedirectText(self.log)
sys.stdout=redir
print 'test'
self.frame.Show()
return True
class RedirectText:
def __init__(self,aWxTextCtrl):
self.out=aWxTextCtrl
def write(self,string):
self.out.WriteText(string)
</code>
If you use wx.App, you can also just set the "redirect" parameter to
True as well.
Mike
More information about the Python-list
mailing list