PyQt GUI with Python
Tags: python
Published: 2020-05-22 09:44:32

PyQt is a cross-platform toolkit for creating GUI applications that integrates <a href="https://python.org">Python</a> with the <a href="https://qt.io">Qt</a> library. 

That is, PyQt allows the use of the Python language to call APl from the Qt library, greatly improving development efficiency while retaining Qt's high operational efficiency.

Because it's much faster to develop programs in Python than in C++, PyQt wraps Qt completely and can do almost anything Qt can do with PyQt.

```python
import sys
from PyQt5.QtWidgets import QApplication,QWidget
  
if __name__ == '__main__':
    app = QApplication(sys.argv) # Create an instance of the QApplication class
    w = QWidget() #Create a window
    w.resize(400,200) #Setting the window size
    w.move(300,300) #move window
    w.setWindowTitle("The first PyQt5-based desktop application") #setWindowTitle
    w.show() #display window
 
    sys.exit(app.exec_()) #Enters the main loop of the program and ensures the main loop ends safely with the exit function
```

![pyqt window](https://dev-to-uploads.s3.amazonaws.com/i/m42u2br1gvra9wash1j7.png)

The function of app.exec_() is to run the main loop, which must be called to start the event processing, and the method is called to enter the main loop of the program until the exit() is called.
 
If you are new to PyQt, I recommend <a href="https://gumroad.com/l/pysqtsamples">this book</a>

## QApplication

The QApplication class manages the control flow and major settings of GUI applications. It can be said that QApplication is the lifeblood of Qt's entire back-office management.

```python
    app = QApplication(sys.argv) # Create an instance of the QApplication class
```

It contains the main event loop in which all events from the window system and other resources are processed and scheduled. 

It also handles the initialization and termination of applications and provides conversation management. It also handles most system-wide and application-wide settings.

For any <a href="https://en.wikipedia.org/wiki/Graphical_user_interface">GUI application</a> that uses <a href="https://qt.io">Qt</a>, there has to be a QApplication object

## QWidget

The QWidget class is the base class for all user interface objects. This includes <a href="https://pythonpyqt.com/pyqt-button/">buttons</a>, <a href="https://pythonbasics.org/pyqt-hello-world/">labels</a>, <a href="https://pythonprogramminglanguage.com/pyqt-line-edit/">text input</a>, <a href="https://pythonpyqt.com/pyqt-progressbar/">progressbar</a>, <a href="https://pythonspot.com/pyqt5-messagebox/">messagebox</a> and a lot more.

## QDialog

The QDialog class is the base class of the dialog window. This lets you create dialogs.

![qdialog](https://dev-to-uploads.s3.amazonaws.com/i/exxrgw63gze2l7qbie9i.png)

## QMainWindow

The QMainWindow class provides a main application window with <a href="https://pythonprogramminglanguage.com/pyqt-menu/">menu bars</a>, <a href="https://pythonbasics.org/pyqt-toolbar/">toolbars</a>, <a href="https://pythonspot.com/pyqt5-statusbar/">status</a> bars (e.g., IDE-Visual Studio, Qt Creator, etc.) that is commonly used for developing <a href="https://qt.io">Qt</a>.

A main window provides the user interface framework for building the application. qt has QMainWindow and its related classes to manage the main window

## QWebEngineView

PyQt5 uses the QWebEngineView control to display HTML pages. Older versions of the <a href="https://pythonprogramminglanguage.com/webkit-browser/">QWebView</a> class are not maintained because QWebEngineView uses the Chromium kernel to give users a better experience.