Learn Python PyQt

PyQt window (Python PyQt5)

This article is about python using PyQt5.

Any desktop app (GUI) needs one or more windows, with PyQt you can create one in Python.

We will show you how to create a basic PyQt window and how to set it up.

Installation of PyQt5

First install PyQt (GUI toolkit) for Python using the pip package manager.

pip install pyqt5

Book: Create Desktop Apps with Python PyQt5

PyQt5 window

1: Create a simple window using PyQt5

import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)
windows = QtWidgets.QWidget()

windows.resize(500,500)
windows.move(100,100)
windows.show()
sys.exit(app.exec_())

This outputs:

pyqt window

You can set its size (width,height) with:

windows.resize(500,500)

Put it anywhere on the screen:

windows.move(100,100)

2: Add titles and icons to created windows

import sys
from PyQt5 import QtWidgets,QtGui

app = QtWidgets.QApplication(sys.argv)
windows = QtWidgets.QWidget()
windows.resize(500,500)
windows.move(100,100)

windows.setWindowTitle('Title')
# set icon
windows.setWindowIcon(QtGui.QIcon('icon.png'))
windows.show()
sys.exit(app.exec_())

This results in an icon set for the window:

python pyqt window with icon

Set the window title with:

windows.setWindowTitle('Title')

Set the icon with:

windows.setWindowIcon(QtGui.QIcon('icon.png'))