tag: pyqt | Python Tutorial

Tag: pyqt

PyQt

Create a graphical user interface with Python. Python is not limited to making terminal applications. Why not make desktop apps?

Applications made with Python and Pyqt work on Windows, Mac and Unix.
There are two major versions of Pyqt: PyQt4 and PyQt5.

PyQt is the most popular GUI module for Python.

Related course:

Install PyQt

To install Pyqt type

pip3 install pyqt5

or type the command:

pip install pyqt5
.

What can I make with PyQt?

Any graphical application. Qt works on Windows, Mac OS X and Linux.

The webbrowser Konqueror is made with Qt:

pyqt app konquerror

pyqt5 hello world

Make a graphical interface with PyQt?

Graphical interfaces made with PyQt run on: Microsoft Windows, Apple Mac OS X and Linux. We will create a Hello World app with PyQt.

pyqt hello world

Related course:

PyQt installation

PyQt is available for both Python 2 (2.7.9 tested) and Python 3.

To install write:

pip3 install pyqt5

With apt-get you can use:

python3-pyqt5

PyQt hello world

The app we write will show the message “Hello World” in a graphical window. Import the PyQt5 module.

# https://pythonprogramminglanguage.com/pyqt5-hello-world/
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtCore import QSize

class HelloWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(640, 480))
self.setWindowTitle("Hello world - pythonprogramminglanguage.com")

centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)

gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)

title = QLabel("Hello World from PyQt", self)
title.setAlignment(QtCore.Qt.AlignCenter)
gridLayout.addWidget(title, 0, 0)

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = HelloWindow()
mainWin.show()
sys.exit( app.exec_() )

The program starts in the main part.

We initialize Qt and create an object of the type HelloWindow.

We call the show() method to display the window.

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = HelloWindow()
mainWin.show()
sys.exit( app.exec_() )

The HelloWindow class inherits from the class QMainWindow.

We call its super method to initialize the window.

Several class variables are set: size and window title.

We add widgets to the window, including a label widget (QLabel) which displays the message “Hello World”.

pyqt5 center window

To center a PyQt window, we need to do a bit of trickery: we need to get the window properties, center point and move it ourself. At the start of the program, it will be in the center of the screen.

Related course:

pyqt center screen

We add QDesktopWidget to the list of imports, having:

from PyQt5.QtWidgets import QMainWindow, QLabel
from PyQt5.QtWidgets import QGridLayout, QWidget, QDesktopWidget

Include this in the window creation method:
qtRectangle = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())

The geometry of the window is then retrieved using:

qtRectangle = self.frameGeometry()

We can output qtRectangle using print(qtRectangle),
this would return PyQt5.QtCore.QRect(0, 0, 640, 480)

Get the center of the screen using

centerPoint = QDesktopWidget().availableGeometry().center()

Set it to the center
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())

pyqt5 button

Add a button in pyqt?

Learn how to add button widgets to an existing Pyqt window. Learn how to connect a click to a Python method.

pyqt button

Related course:

QPushButton

The button widget is called QPushButton.
Start by importing QPushButton

from PyQt5.QtWidgets import QPushButton

In the window constructor, add these lines

pybutton = QPushButton('Click me', self)
pybutton.resize(100,32)
pybutton.move(50, 50)
pybutton.clicked.connect(self.clickMethod)

The first line creates an object of the type QPushButton.

We resize it to 100 pixels in width and 32 in height. Then we set it to position (50,50) on the window. The click must be linked to a Python method, clickMethod().

pyqt5 button example

Complete code resulting in:

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize

class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(300, 200))
self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com")

pybutton = QPushButton('Click me', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(100,32)
pybutton.move(50, 50)

def clickMethod(self):
print('Clicked Pyqt button.')

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )

pyqt5 message box

When creating a Python GUI, you may want to show a message box at some point.

Pyqt comes with messagebox support in both PyQt4 and PyQt5. The class to be used is QMessageBox.

In this tutorial you’ll learn how to show a message box on buton click.

qt message box

Related course:

QMessageBox example

Import QMessageBox from the PyQt5 widgets

from PyQt5.QtWidgets import QMessageBox

A messagebox can easily be added to the window using the code:

QMessageBox.about(self, "Title", "Message")

Complete code:

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import QSize

class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(300, 200))
self.setWindowTitle("PyQt messagebox example - pythonprogramminglanguage.com")

pybutton = QPushButton('Show messagebox', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200,64)
pybutton.move(50, 50)

def clickMethod(self):
QMessageBox.about(self, "Title", "Message")

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )


1234