- Building and Installing wxPython
- I don't know C++, how can I use the docs?
- Can't import wxPython.wx, where is wxPython.wx?
- Can I use wxPython binaries with a different version of Python?
- I've looked at tkInter and at wxPython. Why would I choose one or the other?
- I see a lot of different styles in examples: Which one should I use?
- How to Get Help
- What is the difference between a wxFrame and a wxWindow? When do I want to use each one?
- How do I use multiple threads?
- How do I get out of wxSizerHell ?
- How do I render buttons in a wxGrid?
- How are multiple EVT_XXX calls handled?
- What is the status of wxPython for Mac? How can I help?
- I can see the contents of the splitter window, but they are not active. How can I solve this problem?
- How can I use a visual debugger with wxPython ?
- Why can't I override any C++ method in Python?
- How do I remove a component, or all components of a certain type from a wxFrame/wxPanel?
- How do I turn off tool tips?
- How do I get a wxGrid to update its scrollbars?
- How do I hide the row and column labels in a wxGrid window?
- What's the difference between all the DC types?
- Why doesn't the GetString method of wxCommandEvent work?
- Why do the paths to my resource files work in testing, but fail after installation on Windows?
- How can my EVT_XYZ handler find the control that generated an event?
- How do I use wxSocket* in wxPython?
- How do I embed images in python code?
- How do I keep wxPython from redirecting errors and exceptions to a GUI window?
- Is there a proper way to update wxTreeCtrls and wxListCtrls? (Unanswered)
- Why does the tab key not cycle controls as expected?
- What does "Unicode Build" really mean?
- What is a "Release Series"?
- What is the difference between a stable and an unstable build?
- What are all the different kinds of sizes, and how do they relate to each other?
- Why can't I globally catch exceptions from MainLoop?
- Why doesn't a sizer on a wx.Window work?
- How do I pass in arguments to an event callback?
- Why do I need to Destroy() popup menus and dialogs?
Building and Installing wxPython
How do I build wxPython on Unix?
How do I build wxPython on Windows?
Annotated version of BUILD.html: BuildWxPythonOnWindows
How do I build wxPython on Macintosh?
Which compilers are supported?
Which binary installer should I use?
The one that matches the version of Python you want to use it with.
Python crashes in kernel32.dll when I import wxPython in Windows 95
You can probably fix this by installing winsock2. Download it at http://www.microsoft.com/windows95/downloads/contents/wuadmintools/s_wunetworkingtools/w95sockets2/default.asp
I don't know C++, how can I use the docs?
Can't import wxPython.wx, where is wxPython.wx?
Q. I ran "python demo.py" and all I got was this:
Traceback (most recent call last): File "demo.py", line 3, in ? import Main File "Main.py", line 15, in ? from wxPython.wx import * ImportError: No module named wxPython.wx
When I did a search of my whole Python directory, I found no file named wxpython.wx.
A. In this context the dot means that it is looking for a module named wx located in a Python package named wxPython. Translated to the filesystem that means a directory named wxPython with a file named wx.py located within it. And an empty file named "init.py" to make this folder a package. (fgranger 2002/07/25)
This error message means that the wxPython directory is not located in a directory that is in the PYTHONPATH. To check your path run this from within Python:
import sys print sys.path
If the directory containing the wxPython directory is not there you either need to move wxPython or add the directory to PYTHONPATH. This can be done either in the environment, in a sitecustomize.py file (that is on the default path) on in the registry on Win32. See the Python docs for more details.
On Linux the wxPython RPM for Python 2 puts wxPython in /usr/local/lib/python2.0/site-packages since Python 2 was installed in /usr/local on my build system. If Python 2 is in /usr on your system then you'll want to move (or link) wxPython to /usr/lib/python2.0/site-packages.
On Win32 wxPython will install to the Python directory by default, which is automatically on the PYTHONPATH, so Python will be able to find the modules using the above syntax. If you installed somewhere else then you need to add that directory to the PYTHONPATH as described above.
Can I use wxPython binaries with a different version of Python?
In general, no. You need to use the same version of Python as the wxPython binary was built with. That is why the version is part of the filename, (for example, "Py20")
I've looked at tkInter and at wxPython. Why would I choose one or the other?
tkInter is a GUI toolkit that has been distributed widely with Python, and is readily available to everyone who is running Python. wxPython requires an additional install on top of the Python install. If it's just for your own machine, this isn't a big deal, but if you have a lot of people who need it, this could become a bit of a big deal.
Native Look and Feel
wxPython supports the native look and feel of the platform being used. tkInter has its own look and feel for some components, which doesn't match any standard platform.
Windows XP and Python 2.2.2
In Python 2.2.2 under Windows XP you need this additional step:
Put two files python.exe.manifest and pythonw.exe.manifest, both with the following contents:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="184.108.40.206" processorArchitecture="X86" name="*.*.*" type="win32" /> <description>*</description> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="220.127.116.11" processorArchitecture="X86" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency> </assembly>
in your Python installation directory (where respective .exe files reside). Next time you run your wxPython applications, they'll look like "normal" XP applications.
This may also affect other Python modules that use Windows display API.
If you are using py2exe to build a standalone Python executable, say FOO.EXE, you need to copy pythonw.exe.manifest into the directory where FOO.EXE is and name it FOO.EXE.manifest.
Wide Range of Available Widgets
wxPython has a much wider range of available widgets than does tkInter. There are additions available for tkInter that somewhat mitigate this situation, but this destroys the convenience argument for tkInter. In addition, the wxPython toolkit has support for printing and other non-GUI, but also non-standard operations. tkInter is fine if your application is simply a way of interacting with the user via a GUI, but if you need to develop a full-scale application with support for drawing your own UI elements, support for printing, etc., wxPython is definitely the way to go.
See also Choosing wxPython over Tkinter
I see a lot of different styles in examples: Which one should I use?
see: wxPython Style Guide for advice.
How to Get Help
Is there a mailing list for help?
Yes. You can subscript to the list, and read the archives from:
How do I get help from the wxPython-users mailing list?
When you have a unique problem, it is time to ask a question on wxPython-users. Please conform to the basic rules of netiquet. Aside from them, there is one thing you can do to improve your chances of getting a helpful answer if you have some idea how to do something, but it's not quite working:
Send a Complete, Working App that demonstrates your problem or question. This app should be complete, in that it can be run, but as small as possible, so that it isolates your problem, and gives those of us that read the list as little to look at as possible.
This accomplishes two things:
- When isolating your problem, you might just find out why it wasn't working.
- It makes it MUCH easier for us to help you, for a number of reasons:
- We can just run the app and see how it's behaving.
- We can easily tweak it, and try to find a solution.
- Your problem may not be where you think it is. If you don't send a complete version, your problem may not be in the code you send, or be a function of how that code is used.
You'll find that the folks on wxPython-users are very helpful, and often love to figure out the puzzle your problem presents, but you need to make it as easy on us as possible.
What is the difference between a wxFrame and a wxWindow? When do I want to use each one?
The terminology is a bit confusing. a wxFrame is what most folks think of as a Window: a box on the screen that holds your application, etc. It can be re-sized, moved, etc with the mouse by the window manager of the system.
A wxWindow is the basic unit from which all the controls, etc. are built. It is a rectangle on the screen that captures mouse clicks, draws itself etc. a wxWindow will almost always be put into a Frame or other window.
You want to use a frame to create an independent box on the screen to house some part of your application. You want to use a wxWindow as the basis for a custom control of your own design. All the built-in controls are derived from a wxWindow already.
How do I use multiple threads?
All calls to methods of GUI objects and event handlers need to happen on the main thread. In wxPython the main thread is the one that first imports the wxPython.wx module. Other threads can send events to the main thread using wxPostEvent as is done in the Threads sample in the wxPython demo.
For more information and examples, please see LongRunningTasks.
How do I get out of wxSizerHell ?
Q. I've worked with several GUIs, each has their quirks. While the wxPython demo sort of helps to understand sizers, they don't seem intuitive to me (maybe it's just the way my brain is wired...). Is there any "style guide" or "recipe book" to say what combination of sizers to use to put together particular interface combinations?
A. There are some hints here: UsingSizers
How do I render buttons in a wxGrid?
> I'm doing some work with wxGrids, and I'd like to be able to put a
> button into the grid, so as to pop up a dialog or another window
> that's relevant to the current row.
> I've started fooling around with custom Editors and Renderers, and am
> able to set a wxButton as a custom editor...
> ...I'm also confused as to how to set a button as a custom renderer,
> looking at the example from the demo. Do I actually _need_ any of the
> methods (Draw, Clone, GetBestSize)?
This is where the real problem is. The current design doesn't allow for a control to be the renderer for a cell. The Cell
Renderer needs to be something that draws itself within the cell boundaries, so the best you could do now is draw something that looks like a button. The good news however is that there is already some code to do this that you can borrow from. Look at the generic button controls in wxPython/lib/buttons.py
> > Is there a better way to do this? >
Yes, but I think it will take some modifications to the wxGrid class to enable it. There are some items on my TODO list that I think will help allow you to do things like this.
How are multiple EVT_XXX calls handled?
> I am wondering about how EVT_XXX handles multiple calls. I want to change
> how events are handled depending upon which mode my program is in. If I call
> EVT_XXX multiple times for the same event, each time pointing to a different
> method, are these event handlers chained or do subsequent calls replace the
> old event handler?
Each instance of a class derived from wxEvt
Handler has a static event table, which is used only by C++ code, and a dynamic event table (just a list really) which is what wxPython uses although a few C++ developers use it too. Every time you call a EVT_XXX function it adds an entry to this list. When an event happens the first matching handler in the event table is called, and if it calls event.Skip() then the search continues so if another match is found then it is called too. The EVT_XXX functions are really just convenience wrappers around the wxEvt
Handler.Connect method. There is also a .Disconnect method that you can call if needed to remove an event handler binding.
NB: If you're looking for true many-to-many connecting of event sources and listeners, see the 'wxPython.lib.evtmgr' module.
What is the status of wxPython for Mac? How can I help?
wxPython is in good shape now on OS-X. It is still tested a bit less than other platforms, and as a result has a few more issues, but it is quite functional. Binaries are available on the wxPython download page, as well as www.pythonmac.org/packages
Kevin Altis has set up a wxPythonOSX issue tracking page. You can find it here, but it's pretty out of date:
Of course, if you want to submit patches, I'm sure Robin wouldn't mind... ^_-
I can see the contents of the splitter window, but they are not active. How can I solve this problem?
How can I use a visual debugger with wxPython ?
Most Debuggers (like IDLE or Python Win) use a different GUI toolkit than wxPython does, and some debug the wxPython app within their own process. This creates lots of conflicts between the wxPython event loop and the event loop of the debuggers' GUI tookit.
So, you need a debugger that debugs your program out-of-process. At present, HAP, Wing IDE and Komodo do so. HAP is open-source, so you might be interested in checking that one out; see
Winpd is another good option, wiht a GUI written in wxPython, of course:
Why can't I override any C++ method in Python?
How do I remove a component, or all components of a certain type from a wxFrame/wxPanel?
If your container is using a sizer, you can remove all its children with this function:
To only remove children of a particular kind (ie.wxStaticText), you could add a parameter to the method, and do an isinstance() test. -- RobbShecter
How do I turn off tool tips?
How do I get a wxGrid to update its scrollbars?
In wxPython 18.104.22.168, wxGrid apparently does not update its scroll bars in response to changes in the rows and columns. You can cause it to do this by callingFitInside() on the wxGrid object. For more information, see the wxWidgets reference for the wxScrolledWindow class (wxGrid's base class).
How do I hide the row and column labels in a wxGrid window?
Set the size to 0. The label windows will still exist, but they will not be visible.
(thanks PaulMcNett for the info)
What's the difference between all the DC types?
wxPaintDC can only be used inside a EVT_PAINT event and has extra info about what regions of the window have been damaged and need to be refreshed, allowing some optimization of the re-draw. You can also use this information to optimize your redraw if you want.
wxClientDC can be used anytime for drawing to the window, but you must ensure that whatever you draw then is also redrawn in a EVT_PAINT event otherwise it will get lost the next time the window is refreshed. This is useful if you want to update the screen as a result of some other action, and can be used to display animations, etc.
wxMemoryDC allows you to draw to a wxBitmap instead of to a window. The resulting wxBitmap can be used for a variety of things, including an off screen buffer of what is on screen.
wxBufferedDC and wxBufferedPaintDC are simple convenience classes that derive from wxMemoryDC. You give it a buffer bitmap and then all drawing goes to the bitmap. When the last reference to the buffered dc goes out of scope or is deleted then the contents of the bitmap are dumped to the real DC (if any) that you gave to the buffered dc when you constructed it. This helps to reduce flicker and such for complex drawings. and also reduces the time needed for EVT_PAINT handlers because all they are doing is drawing the buffer bitmap, not a whole complex drawing. You can easily do the same thing yourself without the buffered DCs with just a wxMemoryDC, a bitmap and a DrawBitmap call. See: DoubleBufferedDrawing for an example of it done both ways. There are a few recipes in
RecipesImagesAndGraphics that would be helpful to look at.
Why doesn't the GetString method of wxCommandEvent work?
Many simple controls like buttons, list boxes, check boxes, etc., generate a wxCommandEvent, because the event is often processed by the parent window, and only command events propogate to the parent (if the control itself doesn't handle them). However, because the values needed by the event handler vary depending on the type of the control and on the specific event, wxCommandEvent has a variety of different methods for retrieving the value. Depending on the control and event, not all these methods will return non-zero or non-empty values. For example, GetString only works for EVT_LISTBOX and EVT_CHOICE, but doesn't make any sense for EVT_CHECKBOX. Read the documentation of the wxCommandEvent method for details of which events support that method. If you need additional information, you'll need to retrieve a reference to the control which generated the event (see How can my EVT_XYZ handler find the control that generated an event?)
Why do the paths to my resource files work in testing, but fail after installation on Windows?
This is a known problem. In testing, the relative paths point to the files correctly, but after installation the relative paths do not point to the intended location. To remedy this, you will need to provide a dynamic absolute path to the resource files. Insert the following code in your main Python file:
import sys import os.path import __builtin__ # Sets the homepath variable (you can change the name) to the directory where your application is located (sys.argv). __builtin__.__dict__['homepath'] = os.path.abspath(os.path.dirname(sys.argv))
Whenever you need to load a resource file, you have to append homepath as follows, using os.path.join(). Your application will now load directory names correctly in both testing and post-installation.
toolbar.AddSimpleTool(1, wx.Image(os.path.join(homepath, 'res\\filenew.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'New', '')
How can my EVT_XYZ handler find the control that generated an event?
The event your handler receives will be of a class derived from wxEvent, which has a GetEventObject method. In the case of an event generated by a control, calling GetEventObject() on the event will return a reference to the control.
How do I use wxSocket* in wxPython?
You don't. You should use the Python socket module.
How do I embed images in python code?
Use img2py.py from the tools folder of your wxPython installation. It converts an image file to .png format, compresses it, and stores the binary data in a .py file with routines to load the image. SeeWorkingWithToolBars for an example of how to use it. For automatic updating and maintenance of larger numbers of images, you might consider using
How do I keep wxPython from redirecting errors and exceptions to a GUI window?
Quoting from Getting Started
- When a python program hits an unhandled exception (bug!) in your program, it aborts with a traceback that is useful in locating the source of the problem. wxPython programs do the same, but with a twist. The traceback is routed to stdio, which is captured in a nice GUI frame independent of your program. If an exception shows up in an event handler, the traceback is displayed, and your program continues on as best it can. BUT, if the exception occurs while initializing your program, the traceback will show up, and then your program will abort, taking the stdio window (and your traceback) with it before even the fastest reader can make sense of it.
- You can keep stdio from being hijacked by wxPython by providing a couple of optional parameters when you instantiate your wxApp. An example says it best:
1 myapp = MyApp() # functions normally. Stdio is redirected to its own window 2 myapp = MyApp(0) #does not redirect stdout. Tracebacks will show up at the console. 3 myapp = MyApp(1, 'filespec') #redirects stdout to the file 'filespec' 4 # NOTE: These are named parameters, so you can do this for improved readability: 5 myapp = MyApp(redirect = 1, filename = 'filespec') # will redirect stdout to 'filespec' 6 myapp = MyApp(redirect = 0) #stdio will stay at the console...
Is there a proper way to update wxTreeCtrls and wxListCtrls? (Unanswered)
I'm writing an application which requires fairly frequent updating of a wxTreeCtrl object (e.g. a contact list where people will be coming and going). Is there a right way to do this? Should I just create my tree, show it, and then when I need to update it delete the tree items, add the items again, and show the tree again (this seems to be what PyAIM does, or did)? Does this cause flicker or any other UI oddities? I didn't see an answer to this in ListControls or TreeControls, but that may be the appropriate place to put the final answer to this. Thanks.
Why does the tab key not cycle controls as expected?
Q: I created a sample application with some controls but when I reach the last control and hit the tab key again focus does not return to the first control. What is wrong?
A: Maybe you are using the wxNotebook window. If you place a wxNotebook directly on a frame tab order usually gets confused. Try placing a wxPanel on the wxFrame and put wxNotebook onto this panel. As a rule, simply do not place wxNotebook directly on a wxFrame (except the frame is a wxDialog?). If you use a
WxDialog, tabbing should work as expected. If you use a MDIChildFrame, you must create a Panel first, and then put your controls on the Panel, not directly on the MDIChildFrame.
What does "Unicode Build" really mean?
What is a "Release Series"?
What is the difference between a stable and an unstable build?
What are all the different kinds of sizes, and how do they relate to each other?
Why can't I globally catch exceptions from MainLoop?
Why doesn't a sizer on a wx.Window work?
How do I pass in arguments to an event callback?
Why do I need to Destroy() popup menus and dialogs?