Python | Introduction to PyQt5
There are so many options provided by Python to develop GUI application and PyQt5 is one of them. PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library.
A GUI application consists of Front-end and Back-end. PyQt5 has provided a tool called ‘QtDesigner’ to design the front-end by drag and drop method so that development can become faster and one can give more time on back-end stuff.
First, we need to install PyQt5 library. For this, type the following command in the terminal or command prompt:
pip install pyqt5
If successfully installed one can verify it by running the code:
PyQt5 provides lots of tools and QtDesigner is one of them. For this run this command:
pip install PyQt5-tools
Create your first app –
This is a simple app having a single button in the window. After clicking that button a message will appear “You clicked me”. Let’s start.
- First of all, we need to find QtDesigner to create the front-end part.
– QtDesigner is present at ‘site-packages/pyqt5_tools’
– For finding the location of site-packages write the following python code using any editor of your choice and then run:
>>> import site
– Run the application named ‘designer’.
- A window will open as shown in the figure:
select the ‘Dialog without Button’ option and click ‘Create’
- At the left side of the designer there will be various widgets which can be dragged and dropped in our window according to our requirement.
- Find and drag-and-drop ‘Push Button’ and ‘Label’.
- Change the text inside the widgets by right clicking it and selecting ‘Change plain text’. Keep the text of the Label blank.
- We have created our front-end layout, just save it at your desired location.Remember, this file will have .ui as extension.
- We need to convert the
.pyfile to get the python form of the widgets and attach necessary event listeners to them.
For this we have to go to sitpackages directory in terminal or command prompt and run the command as shown below. Getting the location of sitepackages is mentioned previously.
>>> cd “C:\\Users\\……\\Programs\\Python\\Python36-32\\lib\\site-packages” [Location of sitepackages]
>>> pyuic5 “C:\Users\……\FILENAME.ui”[Exact location of .ui file] -o ” C:\Users\…….\FILENAME.py” [Location where want to put .py file]
- Finally we will add signals and slot in the python code to make it fully functional.
A signal is emitted by widgets after the occurrence of a certain kind of event like a click, Double click, etc.
A slot is any callable function which will perform some action after the occurrence of an event.
- Run the app and click the button.
Below is the code –
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course