There are many graphical user interface (GUI) toolkits that you can use with the Python programming language. The big three are Tkinter, wxPython, and PyQt. Each of these toolkits will work with Windows, macOS, and Linux, with PyQt having the additional capability of working on mobile.
A graphical user interface is an application that has buttons, windows, and lots of other widgets that the user can use to interact with your application. A good example would be a web browser. It has buttons, tabs, and a main window where all the content loads.
In this article, you’ll learn how to build a graphical user interface with Python using the wxPython GUI toolkit.
Here are the topics covered:
- Getting Started with wxPython
- Definition of a GUI
- Creating a Skeleton Application
- Creating a Working Application
Let’s start learning!
Get Your Code: Click here to download the free sample code for the MP3 tag editor you’ll build with wxPython.
Getting Started With wxPython
The wxPython GUI toolkit is a Python wrapper around a C++ library called wxWidgets. The initial release of wxPython was in 1998, so wxPython has been around quite a long time. wxPython’s primary difference from other toolkits, such as PyQt or Tkinter, is that wxPython uses the actual widgets on the native platform whenever possible. This makes wxPython applications look native to the operating system that it is running on.
PyQt and Tkinter both draw their widgets themselves, which is why they don’t always match the native widgets, although PyQt is very close.
This is not to say that wxPython does not support custom widgets. In fact, the wxPython toolkit has many custom widgets included with it, along with dozens upon dozens of core widgets. The wxPython downloads page has a section called Extra Files that is worth checking out.
Here, there is a download of the wxPython Demo package. This is a nice little application that demonstrates the vast majority of the widgets that are included with wxPython. The demo allows a developer to view the code in one tab and run it in a second tab. You can even edit and re-run the code in the demo to see how your changes affect the application.
Installing wxPython
You will be using the latest wxPython for this article, which is wxPython 4, also known as the Phoenix release. The wxPython 3 and wxPython 2 versions are built only for Python 2. When Robin Dunn, the primary maintainer of wxPython, created the wxPython 4 release, he deprecated a lot of aliases and cleaned up a lot of code to make wxPython more Pythonic and easier to maintain.
You will want to consult the following links if you are migrating from an older version of wxPython to wxPython 4 (Phoenix):
- Classic vs Phoenix
- wxPython Project Phoenix Migration Guide
Current releases of wxPython 4 support only Python 3, and wxPython 4.3 requires Python 3.10 or later.
You can now use pip to install wxPython 4, which was not possible in the legacy versions of wxPython. You can do the following to install it on your machine:
$ python -m pip install wxpython==4.3.1 Note: On macOS and Windows, pip installs a prebuilt wheel, so you don’t need a compiler. Linux may also require you to install some dependencies before the pip installer will work correctly.
For example, on Ubuntu you’ll need development packages such as libgtk-3-dev and libgstreamer-plugins-base1.0-dev to get it to install.
Fortunately, the error messages that pip displays are helpful in figuring out what is missing, and you can use the prerequisites section on the wxPython Github page to help you find the information you need if you want to install wxPython on Linux.
There are some Python wheels available for recent Ubuntu versions that you can find on the wxPython GitHub Releases page. To install one of these wheels, you would use the following command:
$ BASE_URL=https://github.com/wxWidgets/Phoenix/releases/download/wxPython-4.3.1 $ WHEEL=wxpython-4.3.1+ubuntu2404-cp313-cp313-linux_x86_64.whl $ python -m pip install -U $BASE_URL/$WHEEL Be sure you have modified the command above to match your version of Linux.
Definition of a GUI
Read the full article at https://realpython.com/python-gui-with-wxpython/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]