Now, the QApplication itself doesn't actually have any windows. The constructor accepts a sys.argv argument since you can also pass in command-line arguments: app = QApplication(sys.argv) ![]() Now, since QApplication is responsible for the initialization of most of the elements involved in developing PyQt applications, we'll want to instantiate it first. Every GUI application you create must have exactly one instance of QApplication. The sys.argv contains a list of all the command line arguments passed to the application. ![]() ![]() Then, we can import QApplication (the basis) and QWidget (the GUI components) from the PyQt6.QtWidgets module: from PyQt6.QtWidgets import QApplication, QWidget In our case, we'll use this module to handle the exit status of the application - when a user hits the "X" button: import sys Let's go over this initialization line-by-line.įirstly, we import the built-in Python sys module that provides us with functions to manipulate the Python Runtime Environment. Running this code initializes a simple application: Let's take a leap and initialize a PyQt application, and initialize a window with an empty canvas: import sysįrom PyQt6.QtWidgets import QApplication, QWidget Note: There is always only one QApplication instance, no matter the number of windows or modal boxes in your application. It handles all of the initialization and the "canvas" we draw on. The entry point of every PyQt application is the QApplication class, which represents the application itself. We'll also be taking a look at the available layout managers and how they influence the positioning of GUI components. The relative order of these Widgets on an application's frame is dictated and managed by a Layout Manager. We'll be going through the most commonly used widgets that will for the most part be present in almost every PyQt application. There are many Widgets, and getting used to them takes time. A widget can be a label, button, menu, combo box, scroller, toolbar, file dialog, etc. Widgets are GUI Components and the core building blocks of user interfaces. One of the core classes of PyQt is the QWidget class - the implementation of a Widget. As PyQt is one of the most commonly used GUI Frameworks for Python, there is both tons of well-written documentation and a large community. ![]() PyQt is a toolkit that is the product of the Qt library and the Python programming language. Should pip install pyqt6 fail, you can check for installation changes here. If you have pip installed onto your system, let's run the following command to install the newest version of PyQt: $ pip install pyqt6 To make use of the PyQt framework we first need to install it using pip package manager. We'll be going through the installation process, and get familiar with the key elements of PyQT, before moving forward to Layout Managers, Widgets, Signals and Slots as well as how to Style Widgets, as well as take a look at UI Files, and how to create UIs via an intuitive drag-and-drop interface, which we can then export into runnable Python scripts: Note: As of writing this tutorial, PyQt6 is the latest and most advanced version of Python's PyQT framework, and also the version we will be using. Popular Python alternatives for developing a GUI include Tkinter, Kivy, PySimpleGUI, and wxPython. In this guide, we'll take a look at how to use Python's PyQT framework to develop a Graphical User Interface (GUI) for a desktop application in Python.
0 Comments
Leave a Reply. |