Redirect RuntimeError to log instead of console in wxPython?
How do I redirect RuntimeError to log instead of the console?
In the code below, the 'Hello' is printed to console, which is redirected to the wx.TextCtrl. The RuntimeError raised OnClose is printed to the terminal however. How do I redirect the RuntimeError to see same log as the 'Hello'?
import sys
import wx
class RedirectText:
def __init__(self, log):
self.log = log
def write(self,string):
self.log开发者_C百科.AppendText(string)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.log = wx.TextCtrl(self, -1, '', style=wx.TE_READONLY|wx.TE_MULTILINE)
sizer = wx.BoxSizer()
sizer.Add(self.log, 1, wx.ALL | wx.EXPAND, 5)
self.SetSizer(sizer)
redirection = RedirectText(self.log)
sys.stdout = redirection
print 'hello'
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, event):
raise RuntimeError('error')
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MainFrame()
frame.Show()
app.MainLoop()
I'd assume you just have to assign your log to sys.stderr.
加载中,请稍侯......
精彩评论