[PyKDE] starting a new PyKDE project

Joseph Rawson umeboshi at gregscomputerservice.com
Thu Jan 18 02:45:20 GMT 2007


On Wednesday 17 January 2007 14:23, Jim Bublitz wrote:
> On Wednesday 17 January 2007 09:22, Joseph Rawson wrote:
> > I do have a quick question.  Is it possible to display a
> > KMessageBox.error on any exception that is raised while the application
> > loop is running?  I know that I can make my own exception classes and
> > have them make the dialogs, but this only helps when I expect an error.
>
> Look at sys.excepthook in the sys module in the Python Library Reference
> docs.. Define an exception handler:
>
>  # 3 arguments required
> def yourExceptionHandler (a,b,c):
> 	print "*** got exception ***"
>
> and beofre starting the event loop do:
>
> sys.excepthook = yourExceptionHandler
>
> The sys..excepthook method (which is now pointed to "yourExceptionHandler")
> gets all unhandled exceptions.
>
> You could, for example, define a method that pops up the KMessageBox as
> part of your KMainWindow subclass, and then call that method from
> "yourExceptionHandler".
>
> An easy test is to define a button that attempts division by zero when
> clicked - "yourExceptionHandler" should get called when you click the
> button, and whatever you code that to do should then happen.
>
> Jim
>
>
>
> _______________________________________________
> PyKDE mailing list    PyKDE at mats.imk.fraunhofer.de
> http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
Hey, that works great!
I've been programming in python for quite a while, but never bothered with 
digging around the sys module very much.  It took me a little while to figure 
out how to print it all out.  Here is what I came up with:


import traceback
from StringIO import StringIO
from kdeui import KMessageBox

separator = '-' * 80

def excepthook(type, value, tracebackobj):
    tbinfofile = StringIO()
    traceback.print_tb(tracebackobj, None, tbinfofile)
    tbinfofile.seek(0)
    tbinfo = tbinfofile.read()
    sections = [str(type), str(value), separator, tbinfo, separator]
    msg = '\n'.join(sections)
    KMessageBox.error(None, msg)
    

and in the main script:

from 'module' import excepthook
sys.excepthook = excepthook

... then __main__
app = myapp()
app.exec_loop()

It seems to work pretty good.  I'll pretty up the message box later, but this 
works quite well for now.

Thanks :)

-- 
Joseph Rawson

(oops, didn't reply to list)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20070118/2224436e/attachment.bin


More information about the PyQt mailing list