Create a Simple GUI Application using wxPython
Last Update : 04 Nov, 2022wxPython is a cross-platform toolkit that is used in Python GUI application development. it is used for creating desktop GUI applications. Also, You can develop applications on Windows, Mac, and various Unix systems using wxPython.
In this tutorial, you will learn to develop your very first GUI application using wxPython. Also, It shows a basic window on your screen with the title "First wxPython Application".
Install wxPython Module
Before starting to develop, you need to install Python and the wxPython modules on your PC. The following shows the steps of installing wxPython on various operating systems.
Install wxPython on Windows
Use the following steps to install wxPython on Windows operating system.
- Type CMD in the search bar and open the Command Prompt application with administrator privileges.
- Type "pip install wxPython" and press Enter.
# This shows the pip command used to
# install wxPython in various situations.
pip install wxPython
# for Python3
pip3 install wxPython
# for Python3
python3 -m pip install wxPython
# if you don't have pip in your PATH
# environment variable
python -m pip install wxPython
# if you get a permissions error
pip install wxPython --user
# using py alias
py -m pip install wxPython
# for Anaconda
conda install -c conda-forge wxpython
Install wxPython on Linux or macOS
If you want to install wxPython on your Linux or macOS or macOS system, you can do so using the following steps.
- Search for "terminal" and start the application.
- Type "pip install wxPython" and press Enter button.
# This shows the pip command used to
# install wxPython in various situations
pip install wxPython
# for Python 3
pip3 install wxPython
# for python 3
python3 -m pip install wxPython
# if you get a permissions error
sudo pip3 install wxPython
# if you don't have pip in your PATH environment variable
python -m pip install wxPython
# alternative if you get permissions error
pip install wxPython --user
# alternative for Debian (Ubuntu)
sudo apt-get install python-wxtools
Alternatively, you can install wxPython from the source. For this, you will need to download the wxPython source code from the wxPython website.
wxPython Simple Example
The following code shows a very simple example. This will show a small GUI window.
import wx
app = wx.App()
frame = wx.Frame(None, title='First wxPython Application')
frame.Show()
app.MainLoop()
This code will not do much. Let's analyze the script line by line.
import wx
The above code line imports the basic wxPython modules for this program. This includes the core, controls, GDI, misc, and windows.
wx is a namespace. Also, all functions and objects from the basic modules should start with a prefix of wx.
app = wx.App()
The above code line will create an application object. Also, Every wxPython program must have one application object.
frame = wx.Frame(None, title='First wxPython Application')
frame.Show()
The above code creates a wx.Frame object. It is an important container widget. We can learn about this widget in detail later.
The wx.Frame widget has no parent itself. Also, it is a parent widget for other widgets. It is a top widget in the hierarchy of widgets. We use None for a parent parameter when the widget has no parents.
After we create the wx.Frame widget, Need to call the Show() method to display the frame on the screen.
app.MainLoop()
The above code shows the last line of our simple program called mainloop. It is an endless cycle. The mainloop can catch and dispatch all events when running our application.
In this chapter, we have successfully created a simple program in wxPython.