在 wxPython 中将 RuntimeError 重定向到日志而不是控制台?
如何将 RuntimeError
重定向到日志而不是控制台?
在下面的代码中,'Hello'
被打印到控制台,该控制台被重定向到 wx.TextCtrl
。然而,OnClose 引发的 RuntimeError
会打印到终端。如何重定向 RuntimeError
以查看与 'Hello'
相同的日志?
import sys
import wx
class RedirectText:
def __init__(self, log):
self.log = log
def write(self,string):
self.log.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()
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.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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您只需将日志分配给 sys.stderr 即可。
I'd assume you just have to assign your log to sys.stderr.