Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否将RuntimeError重定向到日志而不是wxPython中的控制台?_Python_Redirect_Wxpython_Runtime Error - Fatal编程技术网

是否将RuntimeError重定向到日志而不是wxPython中的控制台?

是否将RuntimeError重定向到日志而不是wxPython中的控制台?,python,redirect,wxpython,runtime-error,Python,Redirect,Wxpython,Runtime Error,如何将RuntimeError重定向到日志而不是控制台 在下面的代码中,'Hello'被打印到控制台,控制台被重定向到wx.TextCtrl。但是,关闭时引发的运行时错误将打印到终端。如何重定向运行时错误以查看与'Hello'相同的日志 import sys import wx class RedirectText: def __init__(self, log): self.log = log def write(self,string): s

如何将
RuntimeError
重定向到日志而不是控制台

在下面的代码中,
'Hello'
被打印到控制台,控制台被重定向到
wx.TextCtrl
。但是,关闭时引发的
运行时错误将打印到终端。如何重定向
运行时错误
以查看与
'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()

我假设您只需要将日志分配给sys.stderr。

哈哈!这比我想象的要容易。忽略了那个小细节是愚蠢的。