{PyQT4}集中处理无handle的Exception—sys.excepthook

http://stackoverflow.com/questions/1261668/cannot-override-sys-excepthook


# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class Test(QDialog):
    def __init__(self,parent=None):
        super(Test,self).__init__(parent)
        self.btn1=QPushButton("Button One")
        self.btn2=QPushButton("Button Two")
        self.btn3=QPushButton("Button Three")
        layout=QGridLayout(self)
        layout.addWidget(self.btn1,0,0)
        layout.addWidget(self.btn2,1,0)
        layout.addWidget(self.btn3,2,0)
        self.connect(self.btn1,SIGNAL("clicked()"),self.slotOne)
        self.connect(self.btn2,SIGNAL("clicked()"),self.slotTwo)
        self.connect(self.btn3,SIGNAL("clicked()"),self.slotThree)
    def slotOne(self):
        raise PyQT4_unhandled_exception(self,"In SlotOne")
    def slotTwo(self):
        raise PyQT4_unhandled_exception(self,"In SlotTwo")
    def slotThree(self):
        raise Exception("In SlotThree");
class PyQT4_unhandled_exception(Exception):
    def __init__(self,*args):
        super(PyQT4_unhandled_exception,self).__init__(*args)
def unhandled_exception(type, value, tb):
    if isinstance(value,PyQT4_unhandled_exception):
         QMessageBox.critical(value.args[0], "Error",value.args[1],QMessageBox.Ok)
    else:
        traceback.print_exception(type, value, tb)
sys.excepthook=unhandled_exception
app=QApplication(sys.argv)
dlg=Test()
dlg.show()
sys.exit(app.exec_())

你可能感兴趣的:(exception,layout,Class,import,button,Signal)