GUI toolkits
You can use many GUI toolkits with Python’s programming language. Tkinter (wxPython), PyQt, and PyQt are the three main ones. Each toolkit will work on Windows, macOS, and Linux. PyQt has the added capability to work on mobile.
A graphical user interface (GUI) is an application with buttons, windows, and many other widgets that allow the user to interact with your app. A web browser is a good example. You will find buttons, tabs, and the main window that loads all the content. learn to build python GUI builder to build game in 5 days
This article will show you how to create a graphical user interface using Python with the wax Python GUI Toolkit.
Python provides various options for developing graphical user interfaces (GUIs). Most important are listed below.
Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python. We would look this option in this chapter.
wxPython − This is an open-source Python interface for wxWindows http://wxpython.org.
JPython − JPython is a Python port for Java that gives Python scripts seamless access to Java class libraries on the local machine
These are the topics:
Get Started with WxPython
Definition of a GUI
How to create a Skeleton App
Create a Work Application
Let’s get started!
A sample chapter of Python Tricks is available for download. This book teaches you Python’s best practices and provides simple examples that you can immediately use to create more Pythonic code.
Get Started with wxPython
The wxPython GUI Toolkit wraps Python around a C++ Library called wxWidgets. wxPython’s first release was in 1998. Consequently, wxPython is well-known and has been around for a while. wxPython is different from other toolkits like PyQt and Tkinter in that it uses native widgets whenever possible. This allows wxPython to look native to the operating systems it is running on.
Tkinter and PyQt both draw their widgets by themselves. However, PyQt is very close to the native widgets.
This does not mean that wxPython doesn’t support custom widgets. The wxPython Toolkit includes many custom widgets, as well as dozens upon dozens of core widgets. A section called Extra Files is available on the wxPython Downloads page. It’s worth looking at.
You can download the wxPython demo package here. This application demonstrates many of the widgets included in wxPython. Developers can view and run the code from the demo in two tabs. To see the effects of your changes on the application, you can edit and rerun the code in the demo.
Installation of wxPython
For this article, you will use the most recent wxPython version, also known as the Phoenix release. Only Python 2 is supported by the wxPython3 and wxPython 2 versions. Robin Dunn was the primary maintainer for wxPython. He deprecated a lot aliases and cleaned up a lot code to make wxPython Pythonic easier to maintain.
If you’re migrating from an older version wxPython (Phoenix), you will need to consult these links:
Classic vs Phoenix
wxPython Project Arizona Migration Guide
Python 2.7 and Python 3 are compatible with the wxPython4 package
Pip can now be used to install wxPython 4. This was not possible with the older versions of wxPython. To install it on your computer, you can follow these steps:
$ pip install wxpython
For Mac OS X to be successful, you will need a compiler such as XCode. You may need to install certain dependencies in Linux before the pip installation will work properly.
Pip’s error messages are very helpful in determining what is missing. You can also use the prerequisites section of the wxPython page on Github to find the information you need to install wxPython under Linux.
You can find Python wheels for most of the popular Linux versions in the Extras Linux section. They are available in both GTK2 or GTK3 versions. The following command will be used to install one of these wheels:
Make sure that you have changed the above command to your Linux version.
Definition of a GUI
As mentioned in the introduction, the graphical user interface (GUI), is an interface that’s drawn on the screen and allows the user to interact with it.
Some components of user interfaces are the same:
Main window
Menü
Toolbar
Buttons
Text Entry
Labels
These items are all known as widgets. Many other widgets are available, as well as custom widgets that wxPython support. The widgets will be arranged logically by a developer in a window that the user can interact with.
Event Loops
The graphical user interface waits for the user’s action. An event is a way to describe something. An event is when a user types something while the application is open or when they use their mouse to click a button or another widget.
The GUI toolkit runs an infinite loop, which is known as an event loop. The event loop waits for events and then reacts according to the code of the application. If the application fails to catch an event it ignores that it ever happened.
Programming a graphical user interface requires that each widget be connected to an event handler. This will allow your application to do certain things.
Event loops can be blocked, which is an important consideration. The GUI will appear to freeze and become unresponsive if you block an event loop.
If a process takes more than a quarter of a second to launch in a GUI, it should be launched as a separate thread. This will keep your GUI from freezing and provide a better user experience.
You can communicate with your application using special thread-safe methods from the wxPython framework to notify it that the thread has been finished or give an update.
Let’s make a skeleton app to show how events work.
How to create a Skeleton App
An application skeleton is an interface that doesn’t contain any event handlers. These are great for prototyping. You create the GUI, present it to your stakeholders, and get their approval before you spend too much time on the backend logic.