In this lesson we are going to learn How to Build GUI Window with Python & PyQt6, so before building our gui
window in PyQt6, let’s talk about pyqt6 gui library.
What is Qt ?
Qt is used for developing graphical user interfaces (GUIs) and multi-platform applications that run on all major desktop
platforms and most mobile or embedded platforms. Most GUI programs created with Qt have a native-looking interface,
in which case Qt is classified as a widget toolkit. Non-GUI programs can also be developed, such as command-line tools and
consoles for servers.
Applications using Qt
- OBS a cross-platform screencast software
- Telegram desktop is using Qt
- VLC Media Player is using Qt
- Wireshark a packet analyzer is created by Qt
- Adobe Photoshop Album is using Qt
- CryEngine V is using Qt
- TeamViewer, a computer software package for remote control, desktop sharing, online meetings, web conferencing and file transfer between computers
What is PyQt ?
PyQt is a set of Python bindings for building cross-platform applications, PyQt combines all the advantages of
Qt and Python. With PyQt, you can include Qt libraries in Python code, enabling you to write GUI applications in
Python. In other words, we can say that PyQt allows you to access all the facilities provided by Qt through Python
There are different versions of PyQt in the recent years, we had PyQt4, after that PyQt5 and now we have the latest
version of PyQt and that is PyQt6, according to riverbank computing company, there will be no new update for PyQt4,
and in this course we will just focus on PyQt6 and PyQt5, and the majority part will be related to PyQt6.
Now let’s build our first gui window with Python and PyQt6, but before that we need to install PyQt6.
pip install PyQt6
This is the complete code for this lesson.
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtGui import QIcon
self.setGeometry(200,200, 700, 400)
self.setWindowTitle("PyQt6 - Geekscoders.com")
app = QApplication(sys.argv)
window = Window()
In the above code first we have imported our module and classes, in PyQt everything is module and every module
is divided in to different classes and every class has its own functionality, for example from QtWidgets module we
are importing some classes, now let’s talk about QtWidgets module.
What is QtWidgets Module ?
The QtWidgets module contains classes that provide a set of UI elements to create classic desktop-style user interfaces.
There are about 138 classes that are related to QtWidget Module.
After importing module and classes, we need to create the object of QApplication class, in every PyQt GUI window,
it is important to create one object from QApplication class, now let’s talk about QApplication class.
What is QApplication Class ?
The QApplication class manages the GUI application’s control flow and main settings. QApplication
specializes QGuiApplication with some functionality needed for QWidget-based applications, for any GUI
application using Qt, there is precisely one Application object, no matter whether the application has 0, 1, 2
or more windows at any given time.
if you are using command prompt or command line utility than you can pass sys.argv in QApplication , but if you
don’t want to use you can just give empty list as parameter.
After that we are creating the object of QWidget class, QWidget class is related to QtWidgets module, and it is used
for creating of our window, there are three types of window classes that you can use in PyQt, we have QWidget,
QMainWindow and QDialog, now let’s talk about the difference between these window type classes.
PyQt6 Window Class Types
Every GUI application has a top-level widget and the rest of the widgets are called its children. The top-level widget
can be QDialog, QWidget, or QMainWindow, depending on the template you require.
- QMainWindw: The QMainWindow class provides a main application window,
a main window provides a framework for building an application’s user interface. PyQt has QMainWindow and its related classes for main window management. QMainWindow has its own layout to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar.
- QDialog: The QDialog class is the base class of dialog window and a dialog window is a top-level window mostly used for short-term tasks and brief communications with the user. QDialogs may be modal or modeless.
- QWidget: The QWidget class is the base class of all user interface objects, the widget is the important point of the user interface, it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen.
Now run your complete code and this will be the result.