This article is a tutorial on the python GUI library, PyQt5.
PyQt is actually derived from the famous cross-platform GUI library, Qt. It is the result of combining the versatile Python language with the powerful Qt library. With the help of PyQt5 (it’s latest version) we can create modern, portable and stylish GUI’s for our Python programs.
Another popular alternative is the Tkinter library. While being simple and easy to learn, it is often criticized for it’s rather outdated look and limited features. PyQt5 is considered the more advanced GUI library, and we’ll be demonstrating this in this Tutorial.
PyQt5 Tutorial – Starting out
Unless you already have some basic prior experience with PyQt5, you’ll have to take this PyQt5 setup tutorial first. It explains the very basic commands used to start PyQt for your Python program and how to close it.
Since you’ll find the same commands repeated in every single one of the widgets below, you can skip it if you want, but then you won’t get the proper explanation behind those commands.
Once you’ve learnt a few basic widgets, be sure to check out layout management in PyQt. Learning how to create and use PyQt5 widgets is great, but learning how to position them correctly is even better.
List of QWidgets
A list of widgets covered in our PyQt5 Tutorial.
QLabel – It is one of the simplest widgets in PyQt with the ability to display lines of text. This widget comes with many supporting functions and methods to allow us to retrieve and update this text whenever we want.
QLineEdit – Every GUI needs a way of taking input from the User. In PyQt, the most common way of taking input is through the QLineEdit widget. It offers you a single line where you can input Text.
QPushButton – One of the most basic and common widgets in PyQt5. As the name implies, it’s a button that triggers a function when pushed (clicked).
QRadioButton – Radio Buttons are commonly seen in GUI’s where the user is presented with a list of options. Unlike a CheckBox, only one Radio Button out of many can be picked.
QCheckBox – A Checkbox is an important part of any GUI, used when you want to present the User with a list of options. Unlike Radio buttons, you can select multiple Checkboxes, or even none at all.
QComboBox – This widget presents a drop-down list of items for the user to select an option from. The advantage of this widget is that it takes up very little space on the screen, even if it has alot of different options. This is the widget to be using in space-restricted PyQt GUI’s.
QTextEdit – Like the QLineEdit widget, QTextEdit is used to take input from the user in the form of text. However, unlike QLineEdit which only takes input in a single line, QTextEdit offers a large area where the User can input several lines of text, or even several paragraphs.
QMessageBox – A useful widget used to create GUI dialogs. These dialogs can be used for a variety of purposes and customized in many ways to display different messages, icons and buttons.
QSlider – This widget provides an interactive slider which the user may interact with to select from a range of values. The slider is fully customizable, from it’s range of values to the size of the slider head.
QInputDialog – Alot of GUI’s come with prompts in one or the other. One common use of such prompts or “dialogs” are for taking input from the User. PyQt5 has it’s QInputDialog widget which allows us to create a variety of different input dialogs to take input in many different ways.
QProgressBar – Progress bars is a great way to visualize progression of computer operations such as file transferring, downloading, uploading, copying etc. The alternative would be text-based which is rather boring in comparison.
QDial – Another interactive and visually pleasing way of taking input from the user is through the use of Dials. Used commonly for audio and volume control.
QMediaPlayer – Learn how to create your very own Video Player in Python using the QMediaPlayer widget in PyQt5!
This marks the end of the PyQt5 tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.
If you don’t mind being fed alot of information at once, you can check out the documentation for PyQt.