pyqt4 - How to raise traceback of other class in main class in python -
i coding in python , added trace-backs error handling.
for better understanding, lets have 3 classes.
c1 = main class
traceback = other class
my main class import other classes (traceback) , create instances of these class , proceeds coded. error handling imported traceback
module in 3 classes , updated code within try: except:
working fine desired.
now got idea, keep traceback in main class. other classes imported in main class.
my c1.py looks below
import traceback traceback import c3 class tracebacktest(): def __init__(self): try: self.c3_inst = c3(1,0) self.c3_inst.sub() except exception ex: tracebk = traceback.format_exc() print 'error raised trace back\n%s' % tracebk if __name__ == '__main__': import sys pyqt4 import qtgui app = qtgui.qapplication(sys.argv) win = tracebacktest() sys.exit(app.exec_())
my traceback.py looks below
from pyqt4 import qtgui class c3(qtgui.qmainwindow): def __init__(self,x,y): super(c3,self).__init__() self.a = x self.b = y button = qtgui.qpushbutton('test',self) button.clicked.connect(self.sub) self.show() def sub(self): result = self.a / self.b print 'result %s' % result
in c1.py
after creating instance called self.c3_inst.sub()
immediately, give below trace exception.
error raised trace traceback (most recent call last): file "d:\pbl_data\development\showtime_python\rnd\traceback_1a.py", line 25, in __init__ self.c3_inst.sub() file "d:\pbl_data\development\showtime_python\rnd\traceback.py", line 37, in sub result = self.a / self.b zerodivisionerror: integer division or modulo 0
but when click button in window. below error, not raised exception in c1.py
traceback (most recent call last): file "d:\pbl_data\development\showtime_python\rnd\traceback.py", line 37, in sub result = self.a / self.b zerodivisionerror: integer division or modulo 0
can 1 tell me actual approach trackback work in pyqt connected widget signals.
by time click button, python long past try..except
surrounding self.c3_inst = c3(1,0)
. if want catch exception in c1.py
, you'd need put try..except
around app.exec_()
.
Comments
Post a Comment