gpt4 book ai didi

python - PySide 程序崩溃处理程序

转载 作者:太空宇宙 更新时间:2023-11-03 19:03:20 25 4
gpt4 key购买 nike

我的程序在遇到错误时发送电子邮件。有没有办法处理该程序的崩溃并使用该程序发送有关它的电子邮件?

最佳答案

您正在寻找sys.excepthook,这是一个示例:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import cStringIO, traceback

from PyQt4 import QtGui, QtCore

errorNotice = """
An unhandled exception occurred. Details will be reported
via an automatic email.
"""

class MyMessageBox(QtGui.QMessageBox):
def __init__(self, *args, **kwargs):
super(MyMessageBox, self).__init__(*args, **kwargs)
self.tracebackInfoStream = cStringIO.StringIO()

self.setIcon(QtGui.QMessageBox.Critical)
self.finished.connect(self.on_finished)

def getTraceback(self, exceptionTraceback):
traceback.print_tb(
exceptionTraceback,
None,
self.tracebackInfoStream
)

self.tracebackInfoStream.seek(0)

return self.tracebackInfoStream.read()

def exceptHook(self, exceptionType, exceptionValue, exceptionTraceback):
tracebackInfo = self.getTraceback(exceptionTraceback)

self.setDetailedText(tracebackInfo)
self.setText(errorNotice)
self.exec_()

@QtCore.pyqtSlot()
def on_finished(self):
QtGui.QApplication.quit()

class MyWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)

self.pushButtonError = QtGui.QPushButton(self)
self.pushButtonError.setText("Raise an Error!")
self.pushButtonError.clicked.connect(self.on_pushButtonError_clicked)

self.layoutVertical = QtGui.QVBoxLayout(self)
self.layoutVertical.addWidget(self.pushButtonError)

@QtCore.pyqtSlot()
def on_pushButtonError_clicked(self):
raise UserWarning("This is an error")

if __name__ == "__main__":
import sys

app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')

myMessageBox = MyMessageBox()

sys.excepthook = myMessageBox.exceptHook

main = MyWindow()
main.show()

sys.exit(app.exec_())

关于python - PySide 程序崩溃处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15494825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com