您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何从wxPython应用程序捕获所有异常?

如何从wxPython应用程序捕获所有异常?

为了记录标准输出,可以使用标准输出包装器,例如:

from __future__ import with_statement

class OutWrapper(object):
    def __init__(self, realOutput, logFileName):
        self._realOutput = realOutput
        self._logFileName = logFileName

    def _log(self, text):
        with open(self._logFileName, 'a') as logFile:
            logFile.write(text)

    def write(self, text):
        self._log(text)
        self._realOutput.write(text)

然后,您必须在主Python文件(运行所有文件文件)中对其进行初始化:

import sys    
sys.stdout = OutWrapper(sys.stdout, r'c:\temp\log.txt')

对于记录异常,最简单的MainLoop方法是将wx.App的方法包装在try..except中,然后提取异常信息,以某种方式保存它,然后通过重新引发异常raise,例如:

try:
    app.MainLoop()
except:
    exc_info = sys.exc_info()
    saveExcInfo(exc_info) # this method you have to write yourself
    raise
python 2022/1/1 18:43:50 有312人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶