[PyKDE] Re: PyKDE digest, Vol 1 #437 - 1 msg

Curtis Taylor curtis.taylor at supplysolution.com
Thu Sep 6 18:01:49 BST 2001


Try this:

#!/usr/bin/env python

import sys
from qt import *
            
class PyQMainWindow( QMainWindow ):
    def __init__( self, parent = None, name = None, fl = 0 ):
        QMainWindow.__init__( self, parent, name, fl )
        self.setCaption( self.tr ('Traceback Test') )
        buttonGroup = QButtonGroup( self.tr ('Traceback Test'), self,
'group' )

        global multiLineEdit
        multiLineEdit = QMultiLineEdit( buttonGroup, 'edit' )
        multiLineEdit.insertLine ( "In PyQApplication.exec_loop()..." )
        
        badButton = QPushButton( self.tr ('&Error'), buttonGroup, 'bad'
)
        quitButton = QPushButton( self.tr ('&Quit'), buttonGroup, 'quit'
)

        vBoxLayout = QVBoxLayout( buttonGroup )
        vBoxLayout.addWidget( multiLineEdit )
        vBoxLayout.addWidget( badButton )
        vBoxLayout.addWidget( quitButton )
        
        self.connect( badButton, SIGNAL ('clicked()'), badFunc )
        self.connect( quitButton, SIGNAL ('clicked()'), self, SLOT
('close()') )
        self.setCentralWidget( buttonGroup )

def badFunc():
    try:
        assert bob, 'NameError'
    except NameError, args:
        lines = '%s: %s' % ( args.__class__.__name__, args )
        multiLineEdit.insertLine( lines )

def main( *args ):
    app = QApplication( *args )
    win = PyQMainWindow( None, 'main', Qt.WDestructiveClose )
    win.resize( 300, 50 )
    app.setMainWidget( win )
    win.show()
    app.connect( app, SIGNAL ('lastWindowClosed()'),
                       app, SLOT ('quit()') )
    app.exec_loop()

if __name__ == "__main__":
    main( sys.argv )


-- 
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

On 06 Sep 2001 12:00:04 +0200, pykde-admin at mats.gmd.de wrote:

> 
> Send PyKDE mailing list submissions to
> 	pykde at mats.gmd.de
> 
> To subscribe or unsubscribe via the web, visit
> 	http://mats.gmd.de/mailman/listinfo/pykde
> or, via email, send a message with subject or body 'help' to
> 	pykde-request at mats.gmd.de
> You can reach the person managing the list at
> 	pykde-admin at mats.gmd.de
> 
> When replying, please edit your Subject line so it is more specific than
> "Re: Contents of PyKDE digest..."
> 
> 
> Today's Topics:
> 
>   1. How to capture python tracebacks? (Dave Smith)
> 
> --__--__--
> 
> Message: 1
> Date: Wed, 05 Sep 2001 17:31:43 -0400
> From: Dave Smith <Dave.Smith at transcendata.com>
> Organization: TranscenData -- an ITI Business
> To: PyQt/KDE Mailing List <pykde at mats.gmd.de>
> boundary="------------9D4B0167909B3E56AFB44BFD"
> Subject: [PyKDE] How to capture python tracebacks?
> 
> This is a multi-part message in MIME format.
> --------------9D4B0167909B3E56AFB44BFD
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
> 
> Hello all,
> 
> I'm trying to figure out a way to capture python traceback
> messages that are apparently caught in the pyqt event loop
> and issued to stdout.
> 
> For example, in the attached sample program, I would like
> the tracebacks that occur when the 'Error' button is clicked
> to be displayed in the multi line edit as well as stdout.
> 
> I thought about implementing the event loop in my PyQApplication
> sub-class but could not find enough info to do it.  Has anyone
> else done this?
> 
> Any other ideas?
> 
> Best regards,
> Dave
> 
> 
> --------------9D4B0167909B3E56AFB44BFD
> Content-Type: text/plain; charset=us-ascii;
>  name="qtexc.py"
> Content-Transfer-Encoding: 7bit
> Content-Disposition: inline;
>  filename="qtexc.py"
> 
> #!/usr/bin/env python
> 
> import os
> import sys
> import string
> import traceback
> from qt import *
> 
> class PyQApplication (QApplication):
>    def __init__ (self, argv):
>       QApplication.__init__ (self, argv)
> 
>    def exec_loop (self):
>       global multiLineEdit
>       multiLineEdit.insertLine ("In PyQApplication.exec_loop()...")
>       QApplication.exec_loop (self)
> 
> class PyQMainWindow (QMainWindow):
> 
>    def __init__ (self, parent = None, name = None, fl = 0):
>       QMainWindow.__init__ (self, parent, name, fl)
>       self.setCaption (self.tr ('Traceback Test'))
>       buttonGroup = QButtonGroup (self.tr ('Traceback Test'), self, 'group')
>       vBoxLayout = QVBoxLayout (buttonGroup)
>       global multiLineEdit
>       multiLineEdit = QMultiLineEdit (buttonGroup, 'edit')
>       vBoxLayout.addWidget (multiLineEdit)
>       badButton = QPushButton (self.tr ('&Error'), buttonGroup, 'bad')
>       vBoxLayout.addWidget (badButton)
>       self.connect (badButton, SIGNAL ('clicked()'), badFunc)
>       quitButton = QPushButton (self.tr ('&Quit'), buttonGroup, 'quit')
>       vBoxLayout.addWidget (quitButton)
>       self.connect (quitButton, SIGNAL ('clicked()'), self, SLOT ('close()'))
>       self.setCentralWidget (buttonGroup)
> 
> def getTraceback():
>    lines = traceback.format_exception (sys.exc_type, sys.exc_value,
>       sys.exc_traceback)
>    return string.join (lines, '')
> 
> def badFunc():
>    RaiseNameError 
> 
> def main():
> 
>    try:
>       badFunc()
>    except:
>       print '-'*77
>       print getTraceback()
>       print '-'*77
> 
>    appTopLevel = PyQApplication (sys.argv)
>    appMainWin = PyQMainWindow (None, 'main', Qt.WDestructiveClose)
>    appMainWin.show()
>    appTopLevel.connect (appTopLevel, SIGNAL ('lastWindowClosed()'),
>       appTopLevel, SLOT ('quit()'))
>    appTopLevel.exec_loop()
> 
> if __name__ == "__main__" : main()
> 
> 
> --------------9D4B0167909B3E56AFB44BFD--
> 
> 
> 
> 
> --__--__--
> 
> _______________________________________________
> PyKDE mailing list
> PyKDE at mats.gmd.de
> http://mats.gmd.de/mailman/listinfo/pykde
> 
> 
> End of PyKDE Digest


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20010906/af249bad/attachment.html


More information about the PyQt mailing list