Hi, ALL,
I'm trying to display a non-modal dialog as a custom logger.
Here's what I do:
class Redirect(object):
def __init__(self, textctrl):
self.out = textctrl
def write(self, string):
wx.CallAfter(self.out.WriteText, string )
class LoggingRedirectHandler(logging.StreamHandler):
def __init__(self,textctrl):
logging.StreamHandler.__init__( self )
self.textctrl = textctrl
def emit(self, record):
msg = self.format( record )
stream = self.stream
self.textctrl.WriteText( msg + "\n" )
self.flush
class Executor(wx.Dialog):
def __init__(self, parent, ID, title, options, path_spec,
size=wx.DefaultSize, pos=wx.DefaultPosition,
style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER | wx.THICK_FRAME ):
self.options = options
self.path_spec = path_spec
wx.Dialog.__init__( self, parent, ID, title, pos, size, style )
sizer1 = wx.BoxSizer( wx.VERTICAL )
self.log = wx.TextCtrl( self, -1, "",
wx.DefaultPosition, ( 300, 300 ), wx.TE_MULTILINE )
sizer1.Add( self.log, 1, wx.EXPAND, 0 )
sizer2 = wx.BoxSizer( wx.HORIZONTAL )
self.done = wx.Button( self, -1, "Close Window" )
self.done.Enable( False )
sizer2.Add( self.done, 1, wx.EXPAND |
wx.ALIGN_CENTER_HORIZONTAL, 0 )
sizer1.Add( sizer2, 0, wx.EXPAND, 0 )
self.SetSizer( sizer1 )
sizer1.Fit( self )
def OnShowExecutor(self):
format_str = "[%(levelname)s] %(message)s"
logging.basicConfig( level=logging.INFO, format=format_str )
self.logger = logging.getLogger()
redir = Redirect( self.log )
sys.stdout = redir
sys.stderr = redir
logger_stream_handler = LoggingRedirectHandler( self.log )
self.logger.addHandler( logger_stream_handler )
try:
self.Show()
logging.info("Logging check")
except Exception as e:
print e
Unfortunately the dialog is not displayed properly and hence there is
nothing in the text control.
What am I doing wrong?
Thank you.