There are a few errors that can happen in the creation of your wxPython application object or initial top-level window that can be difficult to track down, especially if you are seeing the error for the first time. The errors listed here are the kind of thing where the error message is not necessarily completely descriptive of the actual problem, or where the error can be difficult to diagnose the first time you see it. These are all fairly common errors for new wxPython programmers.
Here is a troubleshooting guide for some of the most common symptoms:
Symptom Error message at startup saying "unable to import module wx."
Cause—The wxPython module is not in your PYTHONPATH. This means wx. Python may not have been correctly installed. Alternately, if there is more than one version of Python on the system, wxPython may have been installed against a different one than the one you are using.
Solution—Determine which Python version or versions are installed on the machine you are using. On a Unix system, the command which python should tell you the default installation. On a Windows system, you may have to go into the folder options dialog and see what application .py files are assigned to. If wxPython is correctly installed for that Python version, it puts itself in the <python-home>/Lib/site-packages subdirectory. You will likely need to install or reinstall wxPython.
Symptom The application crashes immediately on startup, or a blank window is displayed, followed immediately by an application crash.
Cause—A wxPython object is created or used before the creation of the wx.App.
Solution—Create the wx.App object immediately on starting your script.
Symptom My top-level windows are created and immediately close. The application exits immediately.
Cause—The method wx.App.MainLoop() was not called.
Solution—Call the MainLoop() method after all your setup is complete.
Symptom My top-level windows are created and immediately close. The application exits immediately, and I did call MainLoop().
Cause—An error in your application's Onlnit() method, or some method called by it (such as a frame_init__() method).
Solution—An error before MainLoop() is called can trigger an exception that exits the program. If your application is set to redirect stdout and stderr to windows, then those windows will not display long enough for you to see the error. Create your application object with the redirect=False option to allow you to see the error message. See the section "Redirecting Output" for more details.
Was this article helpful?