<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/0.11.1">
</HEAD>
<BODY>Try this:<br>
<br>
#!/usr/bin/env python<br>
<br>
import sys<br>
from qt import *<br>
<br>
class PyQMainWindow( QMainWindow ):<br>
def __init__( self, parent = None, name = None, fl = 0 ):<br>
QMainWindow.__init__( self, parent, name, fl )<br>
self.setCaption( self.tr ('Traceback Test') )<br>
buttonGroup = QButtonGroup( self.tr ('Traceback Test'), self, 'group' )<br>
<br>
global multiLineEdit<br>
multiLineEdit = QMultiLineEdit( buttonGroup, 'edit' )<br>
multiLineEdit.insertLine ( "In PyQApplication.exec_loop()..." )<br>
<br>
badButton = QPushButton( self.tr ('&Error'), buttonGroup, 'bad' )<br>
quitButton = QPushButton( self.tr ('&Quit'), buttonGroup, 'quit' )<br>
<br>
vBoxLayout = QVBoxLayout( buttonGroup )<br>
vBoxLayout.addWidget( multiLineEdit )<br>
vBoxLayout.addWidget( badButton )<br>
vBoxLayout.addWidget( quitButton )<br>
<br>
self.connect( badButton, SIGNAL ('clicked()'), badFunc )<br>
self.connect( quitButton, SIGNAL ('clicked()'), self, SLOT ('close()') )<br>
self.setCentralWidget( buttonGroup )<br>
<br>
def badFunc():<br>
try:<br>
assert bob, 'NameError'<br>
except NameError, args:<br>
lines = '%s: %s' % ( args.__class__.__name__, args )<br>
multiLineEdit.insertLine( lines )<br>
<br>
def main( *args ):<br>
app = QApplication( *args )<br>
win = PyQMainWindow( None, 'main', Qt.WDestructiveClose )<br>
win.resize( 300, 50 )<br>
app.setMainWidget( win )<br>
win.show()<br>
app.connect( app, SIGNAL ('lastWindowClosed()'),<br>
app, SLOT ('quit()') )<br>
app.exec_loop()<br>
<br>
if __name__ == "__main__":<br>
main( sys.argv )<br>
<pre>--
Curtis Taylor
SupplySolution, Inc.
911 Olive Street
Santa Barbara CA 93101
805.879.7264
PGP Fingerprint: 5A61 444C 71C2 8EAE 7268 A78A F41F A503 00A5 234D</pre>On 06 Sep 2001 12:00:04 +0200, pykde-admin@mats.gmd.de wrote:<pre><FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> Send PyKDE mailing list submissions to</FONT>
<FONT COLOR="#737373">>         pykde@mats.gmd.de</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> To subscribe or unsubscribe via the web, visit</FONT>
<FONT COLOR="#737373">>         http://mats.gmd.de/mailman/listinfo/pykde</FONT>
<FONT COLOR="#737373">> or, via email, send a message with subject or body 'help' to</FONT>
<FONT COLOR="#737373">>         pykde-request@mats.gmd.de</FONT>
<FONT COLOR="#737373">> You can reach the person managing the list at</FONT>
<FONT COLOR="#737373">>         pykde-admin@mats.gmd.de</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> When replying, please edit your Subject line so it is more specific than</FONT>
<FONT COLOR="#737373">> "Re: Contents of PyKDE digest..."</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> Today's Topics:</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> 1. How to capture python tracebacks? (Dave Smith)</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> --__--__--</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> Message: 1</FONT>
<FONT COLOR="#737373">> Date: Wed, 05 Sep 2001 17:31:43 -0400</FONT>
<FONT COLOR="#737373">> From: Dave Smith <Dave.Smith@transcendata.com></FONT>
<FONT COLOR="#737373">> Organization: TranscenData -- an ITI Business</FONT>
<FONT COLOR="#737373">> To: PyQt/KDE Mailing List <pykde@mats.gmd.de></FONT>
<FONT COLOR="#737373">> boundary="------------9D4B0167909B3E56AFB44BFD"</FONT>
<FONT COLOR="#737373">> Subject: [PyKDE] How to capture python tracebacks?</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> This is a multi-part message in MIME format.</FONT>
<FONT COLOR="#737373">> --------------9D4B0167909B3E56AFB44BFD</FONT>
<FONT COLOR="#737373">> Content-Type: text/plain; charset=us-ascii</FONT>
<FONT COLOR="#737373">> Content-Transfer-Encoding: 7bit</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> Hello all,</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> I'm trying to figure out a way to capture python traceback</FONT>
<FONT COLOR="#737373">> messages that are apparently caught in the pyqt event loop</FONT>
<FONT COLOR="#737373">> and issued to stdout.</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> For example, in the attached sample program, I would like</FONT>
<FONT COLOR="#737373">> the tracebacks that occur when the 'Error' button is clicked</FONT>
<FONT COLOR="#737373">> to be displayed in the multi line edit as well as stdout.</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> I thought about implementing the event loop in my PyQApplication</FONT>
<FONT COLOR="#737373">> sub-class but could not find enough info to do it. Has anyone</FONT>
<FONT COLOR="#737373">> else done this?</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> Any other ideas?</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> Best regards,</FONT>
<FONT COLOR="#737373">> Dave</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> --------------9D4B0167909B3E56AFB44BFD</FONT>
<FONT COLOR="#737373">> Content-Type: text/plain; charset=us-ascii;</FONT>
<FONT COLOR="#737373">> name="qtexc.py"</FONT>
<FONT COLOR="#737373">> Content-Transfer-Encoding: 7bit</FONT>
<FONT COLOR="#737373">> Content-Disposition: inline;</FONT>
<FONT COLOR="#737373">> filename="qtexc.py"</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> #!/usr/bin/env python</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> import os</FONT>
<FONT COLOR="#737373">> import sys</FONT>
<FONT COLOR="#737373">> import string</FONT>
<FONT COLOR="#737373">> import traceback</FONT>
<FONT COLOR="#737373">> from qt import *</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> class PyQApplication (QApplication):</FONT>
<FONT COLOR="#737373">> def __init__ (self, argv):</FONT>
<FONT COLOR="#737373">> QApplication.__init__ (self, argv)</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> def exec_loop (self):</FONT>
<FONT COLOR="#737373">> global multiLineEdit</FONT>
<FONT COLOR="#737373">> multiLineEdit.insertLine ("In PyQApplication.exec_loop()...")</FONT>
<FONT COLOR="#737373">> QApplication.exec_loop (self)</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> class PyQMainWindow (QMainWindow):</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> def __init__ (self, parent = None, name = None, fl = 0):</FONT>
<FONT COLOR="#737373">> QMainWindow.__init__ (self, parent, name, fl)</FONT>
<FONT COLOR="#737373">> self.setCaption (self.tr ('Traceback Test'))</FONT>
<FONT COLOR="#737373">> buttonGroup = QButtonGroup (self.tr ('Traceback Test'), self, 'group')</FONT>
<FONT COLOR="#737373">> vBoxLayout = QVBoxLayout (buttonGroup)</FONT>
<FONT COLOR="#737373">> global multiLineEdit</FONT>
<FONT COLOR="#737373">> multiLineEdit = QMultiLineEdit (buttonGroup, 'edit')</FONT>
<FONT COLOR="#737373">> vBoxLayout.addWidget (multiLineEdit)</FONT>
<FONT COLOR="#737373">> badButton = QPushButton (self.tr ('&Error'), buttonGroup, 'bad')</FONT>
<FONT COLOR="#737373">> vBoxLayout.addWidget (badButton)</FONT>
<FONT COLOR="#737373">> self.connect (badButton, SIGNAL ('clicked()'), badFunc)</FONT>
<FONT COLOR="#737373">> quitButton = QPushButton (self.tr ('&Quit'), buttonGroup, 'quit')</FONT>
<FONT COLOR="#737373">> vBoxLayout.addWidget (quitButton)</FONT>
<FONT COLOR="#737373">> self.connect (quitButton, SIGNAL ('clicked()'), self, SLOT ('close()'))</FONT>
<FONT COLOR="#737373">> self.setCentralWidget (buttonGroup)</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> def getTraceback():</FONT>
<FONT COLOR="#737373">> lines = traceback.format_exception (sys.exc_type, sys.exc_value,</FONT>
<FONT COLOR="#737373">> sys.exc_traceback)</FONT>
<FONT COLOR="#737373">> return string.join (lines, '')</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> def badFunc():</FONT>
<FONT COLOR="#737373">> RaiseNameError </FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> def main():</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> try:</FONT>
<FONT COLOR="#737373">> badFunc()</FONT>
<FONT COLOR="#737373">> except:</FONT>
<FONT COLOR="#737373">> print '-'*77</FONT>
<FONT COLOR="#737373">> print getTraceback()</FONT>
<FONT COLOR="#737373">> print '-'*77</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> appTopLevel = PyQApplication (sys.argv)</FONT>
<FONT COLOR="#737373">> appMainWin = PyQMainWindow (None, 'main', Qt.WDestructiveClose)</FONT>
<FONT COLOR="#737373">> appMainWin.show()</FONT>
<FONT COLOR="#737373">> appTopLevel.connect (appTopLevel, SIGNAL ('lastWindowClosed()'),</FONT>
<FONT COLOR="#737373">> appTopLevel, SLOT ('quit()'))</FONT>
<FONT COLOR="#737373">> appTopLevel.exec_loop()</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> if __name__ == "__main__" : main()</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> --------------9D4B0167909B3E56AFB44BFD--</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> --__--__--</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> _______________________________________________</FONT>
<FONT COLOR="#737373">> PyKDE mailing list</FONT>
<FONT COLOR="#737373">> PyKDE@mats.gmd.de</FONT>
<FONT COLOR="#737373">> http://mats.gmd.de/mailman/listinfo/pykde</FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> </FONT>
<FONT COLOR="#737373">> End of PyKDE Digest</FONT></pre><TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<pre></pre></TD>
</TR>
</TABLE>
</BODY>
</HTML>