Python wx module

Python wx module Introduction

Python provides us a wxpython module which we usually call as Python wx module. The wxpython module is allow us to create a very high functional GUI (Graphical User Interface). It is developed by Harri Pasanen along with Robin Dunn. Actually, the wxpython module is a wrapper of Python for the wxWidgets that is written and developed in C++ programming language.

Features of Python wx Module

Following are some important key points for the Python wx module:

  • The wxpython module is an open source module. It means it is available for everyone and that too free of cost.
  • Any Python developer can work on wxpython module and edit or modify it.
  • The update of wxpython module can also be pushed to all the users through an open-source community.
  • wxpython module is cross platform Python module that is used for the development of interactive GUI.

Prerequisites of wxpython module:

To learn wxpython module with better understanding, we should have a basic understanding of Python programming language. You can also refer to our Python tutorial to learn Python.

Installation of wxpython module:

We have to install wxpython in our system before using it in our Python program. Only after installing wxpython module in our system, we can implement and can build an interactive GUI (Graphical User Interface) with it.

We have to follow the given following steps properly to install the wxpython module in our system in a correct way:

Step 1: Open the command prompt (cmd) in the system. Before starting the installation, we need to ensure that our system is connected to a network connection so that we can install wxpython module without any error.

Python wx module

Step 2: Write down the following command in command prompt and press the enter key:

> pip install wxpython

When we press the enter key, the installation of wxpython module will be started with the help of pip installer and we can see the following window in our command prompt:

Python wx module

Step 3: After the installation is completed and following success message is shown in the command prompt, Exit the command prompt.

Python wx module

Key points to remember before creating GUI with wxpython module:

Following are the key points that we should remember before using wxpython module in our Python program and creating an interactive GUI with it:

1. First we have to import wxpython module in our Python program.

2. Then we have to create a Python object for the application class of GUI in our Python program.

3. Then we have to create a Python obejct for the frame class of GUI. Other controls of GUI are added to frame class through this object which we will create.

4. After that we will have to create a static object in our Python program through which we will show text object in the GUI.

5. Then we have to use the show method in our Python program to show the frame window in the GUI.

6. Now, we have to run our program or application till the GUI window is closed by using the main application object we created.

Now, let's see an example through which we can better understand that how can we create a GUI through wxpython module.

Example 1: A Python program for a simple GUI (Graphical User Interface) build through wxpython module and shown 'TUTORIALANDEXAMPLE' in output:

 # import wx module in our Python program
 import wx
 # creaing application object with the Python object
 Application1 = wx.App()
 # creating a frame with the Python object
 frame = wx.Frame(None, title ="TUTORIALANDEXAMPLE")
 panel = wx.Panel(frame)
 # Adding a text to the frame object with statictext() function
 text1 = wx.StaticText(panel, label ="Hello Python Developers, This is a simple GUI for TutorialandExample!", pos =(100, 50))
 # show the application by using the show() function
 frame.Show()
 # start the event loop to run the application
 Application1.Mainloop() 

Output:

Python wx module

Explanation: In the above Python program we can see that, we have imported wxpython module in our Python program. Then we have created a Python object in our program that will be treated as an application object in the program.

After that, we have created another Python object that will be used to add frames in our GUI. After that, we have used another Python object and this object is used to add the static text object in the GUI that we are building.

Then, we have used the show() function in our Python program to show the GUI in the output. And in the last, we have used Mainloop() function by which we have run the program and created the GUI in the result. In the output,  we can see that we have created a GUI with a frame and a simple text object in it i.e., "Hello Python Developers, This is a simple GUI for TutorialandExample!".