Learn Python PyQt

PyQt window (Python PyQt5)

PyQt Window Creation with Python Using PyQt5

When developing desktop applications, GUI windows are essential. With Python’s PyQt5 library, setting up a window is straightforward and powerful. This article walks you through the steps to create and customize a basic PyQt window.

Installing PyQt5

Begin by installing the PyQt GUI toolkit for Python using the pip package manager:

pip install pyqt5

If you’re looking to dive deeper into creating desktop applications with PyQt5, check out this Book: Create Desktop Apps with Python PyQt5.

Creating a Basic PyQt5 Window

Here’s a step-by-step guide:

1: Setting Up a Simple Window in 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_())

PyQt basic window

To customize the window’s size and position:

  • Setting Size: Use the resize method:

    windows.resize(500,500)
    
  • Setting Position: Use the move method:

    windows.move(100,100)
    

2: Adding Titles and Icons to the Window

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')
windows.setWindowIcon(QtGui.QIcon('icon.png'))
windows.show()
sys.exit(app.exec_())

This code adds both a title and an icon to the window:

Python PyQt window with icon

To further customize the window:

  • Setting the Title: Use the setWindowTitle method:

    windows.setWindowTitle('Title')
    
  • Setting the Icon: Use the setWindowIcon method:

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