How to debug wxPython errors when your applications crash

Posted in howto , python

If you call wxpython code and don’t catch your errors and do something with them before your app crashes, you lose the errors. Whether or not you do anything before failing is up to you, but how are you going to do anything about it if you have no idea why it’s failing? If you want to know why the code isn’t working, you need to build a function to display the errors when they’re caught.

import wx
import sys
import traceback

def show_error():
    message = ''.join(traceback.format_exception(*sys.exc_info()))
    dialog = wx.MessageDialog(None, message, 'Error!', wx.OK|wx.ICON_ERROR)
    dialog.ShowModal()

Subclass your wx objects here…

After your done building your wx objects and you’re ready to show it and call MainLoop(), wrap your main loop where you actually instantiate your gui objects in try/accept statements so that you can really catch any errors by calling the “show_errors()” function to launch a new message window where the errors will get displayed. This lets you catch errors before your whole program dies (causing errors to get lost).

def main():
    app = wx.App()
    try:
        frame = MyFrame()
        frame.Show()
        app.MainLoop()
    except:
        show_error()

if __name__ == '__main__':
    main()
Posted by admica   @   20 October 2009
Tags : , , , , ,

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

1 Comments

Comments
Dec 22, 2010
9:12 am
#1 Bernard :

Worked like a charm, thank you! Being unable to see the wxpython errors was getting me nowhere!

Leave a Comment

Name

Email

Website

Previous Post
« Tail a file in Python
Next Post
Disable oplocks in a heterogeneous Samba / NFS environment »
Powered by Wordpress   |   Lunated designed by ZenVerse