1. Description of PyQt5 library

PyQt is a set of Python bindings for the Qt application framework and runs on all platforms supported by Qt including Windows, OS X, Linux, iOS, and Android. PyQt is available under the GPL and commercial licenses. The GPL version is free of charge for open source projects, while the commercial version has a fee. PyQt is developed by Riverbank Computing, a UK-based company. PyQt allows you to create desktop applications with Python bindings for the Qt library. It is a popular choice for creating cross-platform GUI applications, and is often compared to Tkinter, which is the standard Python library for creating GUI applications. PyQt offers a more powerful set of widgets and is better suited for larger and more complex applications.

2. How to install PyQt5

PyQt5 can be installed using pip, the Python package installer. You can install it by running the following command in your command prompt or terminal:

# install PyQt5 library
pip install PyQt5
# install PyQt( tools
pip install pyqt5-tools
  • If you are using a version of Python earlier than 3.4, you may need to use pip3 instead of pip.
  • In addition to the PyQt5 package, you will also need to install the Qt libraries and tools. The easiest way to do this is to download and install the official Qt installer for your operating system from the Qt website.
  • Once you have the installer, you can run it and select the version of Qt and the components you want to install. Be sure to select the version that corresponds to the version of PyQt5 you have installed.
  • If you are on Windows or Mac, the installer will also install a version of the Mingw or clang compiler, this is needed to build the C++ code of PyQt5.

Remark

Please note that on Windows, it's important that you use the same version of python and the same bitness (32 or 64bits) for PyQt5 and the Qt libraries. After installing the dependencies, you should be able to import and use the PyQt5 modules in your Python scripts.

3. List Of PyQt5 widgets

Here is a list of some commonly used widgets in PyQt5 with a brief description of their functionality:

  1. QLabel: Displays text or an image.
  2. QLineEdit: Allows the user to enter and edit a single line of text.
  3. QTextEdit: Allows the user to enter and edit multiple lines of text.
  4. QPushButton: A button that can be clicked to perform an action.
  5. QCheckBox: A checkbox that can be checked or unchecked.
  6. QRadioButton: A radio button that can be selected or deselected.
  7. QComboBox: A drop-down list that allows the user to select one item from a list.
  8. QListWidget: A list box that displays a list of items.
  9. QTreeWidget: A tree-like list box that displays items in a hierarchical structure.
  10. QTableWidget: A table that displays items in a grid.
  11. QProgressBar: A widget that displays the progress of a task.
  12. QSlider: A widget that allows the user to select a value by sliding a thumb along a track.
  13. QSpinBox: A widget that allows the user to select a value by clicking up and down arrows or typing a value into a text box.
  14. QDateEdit: A widget that allows the user to select a date.
  15. QTimeEdit: A widget that allows the user to select a time.
  16. QDateTimeEdit: A widget that allows the user to select a date and time.
  17. QGroupBox: A container widget that groups related widgets together.
  18. QTabWidget: A widget that allows the user to switch between multiple pages of widgets, each represented by a tab.
  19. QDialog: A dialog box that can be used to display information or ask the user for input.
  20. QMainWindow: A main window that can contain menus, toolbars, and a central widget.
  21. QGraphicsView: A widget that can display 2D graphics, such as images, shapes, and text.
  22. QStackedWidget: A widget that allows the user to switch between multiple pages of widgets, where only one widget is visible at a time.
  23. QToolBar: A toolbar that can contain buttons and other widgets, typically used to provide access to frequently used actions.
  24. QAction: A class that represents an action that can be added to menus, toolbars, or other widgets.
  25. QMenu: A widget that represents a menu, which can be added to a menu bar or other widget.
  26. QMenuBar: A widget that represents a menu bar, typically located at the top of a main window.
  27. QStatusBar: A widget that displays status information, typically located at the bottom of a main window.
  28. QSplitter: A widget that allows the user to resize multiple widgets by dragging a splitter handle.
  29. QMdiArea: A widget that allows the user to open multiple windows within a single parent window, similar to how multiple documents can be opened in an application like Microsoft Word.
  30. QFileDialog: A dialog box that allows the user to select a file or a directory.

These are just a few examples of the many widgets available in PyQt5, it's very rich in terms of widgets, you can find more information on the specific functionality and usage of each widget in the PyQt5 documentation.

4. First PyQt5 App

To create a graphics window in PyQt5, we must:

  1. Import the system module: sys
  2. Import the QApplication class that generates the application: QApplications from the PyQt5.QtWidgets package
  3. Import the QWidget class: from the PyQt5.QtWidgets package
  4. Create an application: using the instance method of the QApplication class
  5. Create a window: with the QWidget() method: win = QWidget ()
  6. View the window: using the show() method: win.show ()
  7. Run the application: using the exec_() method: app.exec_ ()

Here is an example of a simple PyQt5 application

Example

import sys
from PyQt5.QtWidgets import QApplication, QWidget

# create a PyQt5 application
app = QApplication(sys.argv)

# create a QWidget object
win = QWidget()

# Make a window title
win.setWindowTitle("This is a first PyQt5 app !")

# define a geometry of the window
win.setGeometry(100 , 100 , 500 , 400)

# apply the show method to view the window
win.show()

sys.exit(app.exec_())

 

Here is an other example of a basic PyQt5 application that creates a simple window with a "Quit" button:

Example

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import QCoreApplication

class MyApp(QWidget):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
quit_button = QPushButton('Quit', self)
quit_button.clicked.connect(QCoreApplication.instance().quit)
quit_button.resize(quit_button.sizeHint())
quit_button.move(100, 50)

self.setWindowTitle('My First PyQt5 App')
self.setGeometry(300, 300, 300, 150)
self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())

 

In this example:

  1. we first import the necessary modules from PyQt5.
  2. Then, we create a custom class called MyApp, which is based on the QWidget class.
  3. The MyApp class defines a constructor method (init) that sets up the user interface
  4. and finally an initUI method that creates the "Quit" button and sets the title and dimensions of the window.
  5. In the main program, we create an instance of the QApplication class, which manages the application's control flow and main settings.
  6. We also create an instance of our custom MyApp class and call its show() method to display the window.
  7. The exec_() method is called to start the event loop and execute the application.
  8. The sys.exit() function is called to ensure a clean exit when the application is closed.

This is a simple example, but it demonstrates the basic structure of a PyQt5 application and how to create a window with a button. you can find more information and examples in the PyQt5 documentation.

Younes Derfoufi
my-courses.net

Leave a Reply