wxpython log redirect

V

vedrandekovic

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
 
K

kyosohma

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top