Any QT application is made up of widgets. Qt provides several built-in widgets which we can use in designing our GUI application.
What is an Event Handling GUI applications are meant to be used for user interactions. User interacts with the help of mouse, keyboard, touch etc. and typical user actions are clicking, dragging, resizing etc. Whenever an user interacts with a GUI, some events are triggered like mouse clicked, mouse dragged, key pressed, screen touched. These events are then processed by Qt event handling mechanism.
There are several types of events which can be implemented in a GUI applications. So we need to register only those events which we are going to use in our application. Handlers related to those events are implemented in the code. Whenever an event is fired, it’s related handler immediately captures the command of execution and do the processing which we want it to do.
In Qt, these events are called Signals and event handlers are called Slots.
To understand Signal and slot in a better way let’s place a QLabel widget and a QPushButton widget in our GUI application
In this application : Event we want to capture : Button Pressed Task we want to do : Update the label text
As we are going to add two widgets in our GUI. We need to tell Qt about the position of these widgets. This can be done using Qt Layouts. We will discuss about layouts in detail in next lessons. For now here we are using QVerticalLayout which will place the widgets in vertical manner
To test and run the program click on Run menu item and click Run Without Debugging or just press shortcut Ctrl+F5
A window will popup which is the result of this simple Python Qt GUI program.
Let’s now understand this basic Gui program.
Import statements loads the library modules which can be used in our program further.
Next the line app = QApplication(sys.args) creates an QApplication object. Every Gui program in Qt require exactly one instance of QApplication.
Then we make one object of QWidget window = Qwidget()
QWidget class is the base class of all user interface objects in Qt. To show this widget on our Gui, we need to use show() function.
window.show() will display the widget on screen.
To change the title of GUI window we can use window.setWindowTitle(“My Python GUI”)
In the end we have used app.exec_() which starts the event loop and program is not reached up to the end. If we don’t use this function, program will immediately reach to end making the window appear for a moment only.
Here we are using conda virtual environment for learning Python Qt programming. Here is how we can install PyQt library withing conda virtual environment.
Let us start by creating a new virtual environment named “pythonQt” (You can name it anything you want).
1. Start Anaconda navigator. Click on Tab 2. Click on 3. Write name of new virtual environment as pythonQt in Name field. 4. Click on Create .
After few moments new virtual environment will be installed.
To use the new pythonQt virtual environment. Click on Home tab and select virtual environment from drop down.
Next step is to install PyQt library within our virtual environment.
start anaconda prompt in windows (or terminal in Linux) and activate new virtual environment “pythonQt” by typing following command.
conda activate path-to-virtualEnv
pip install pyqt5
Now pyqt5 is installed successfully. We are going to use Visual Studio Code IDE for developing our PyQt applications.
Start VS Code IDE from anaconda navigator. When we launch VS Code IDE from anaconda navigator, VS code automatically utilizes python and installed libraries from the selected virtual environment. In our case pythonQt virtual environment.
If the desired virtual environment is not loaded then click on the bottom and select the one from the list.