pyqt5 hello world

PyQt can be used to create a graphical user interface, GUI. Qt was created by Riverbank Computing.
Graphical interfaces made with PyQt can run on all major desktop systems including Microsoft Windows, Apple Mac and Unix.

We will create a Hello World app with PyQt.

pyqt hello world
Python GUI using PyQt: hello world

Related course

Install PyQt

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.

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") 
        
        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 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.

pyqt-messagebox
PyQt messagebox

Related course:
Python Programming Full Course (Basics, OOP, Modules, PyQt)

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_() )

 

pyqt5 tooltip

Pyqt supports tool tips, they can be configured for widgets.
A tooltip is a hint in a graphical interface. Tooltips frequently appear when hovering the mouse over a widget (without clicking). Tooltips are not shown on mobile devices because there’s no mouse cursor.

pyqt-tooltip
PyQt tooltip example

Related course
Python Desktop Application Development with PyQt

Pyqt5 tooltip

A tooltip can be set using a widgets setTooltip method.

pybutton.setToolTip('This is a tooltip for the QPushButton widget')  

The setToolTip() method sets the tooltip message for a QtWidget.

Copy/paste example:

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QPushButton
from PyQt5.QtCore import QSize    
     
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
 
        self.setMinimumSize(QSize(300, 100))    
        self.setWindowTitle("PyQt tooltip example - pythonprogramminglanguage.com") 
        
        pybutton = QPushButton('Pyqt', self)
        pybutton.clicked.connect(self.clickMethod)
        pybutton.resize(100,32)
        pybutton.move(50, 20)        
        pybutton.setToolTip('This is a tooltip message.')  
        
    def clickMethod(self):
        print('PyQt')

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

 

PyQt line edit

A textbox or LineEdit can be created using the QLineEdit class. Many applications have text inputs like form fields and the like.

In this tutorial we’ll make an app that displays the name typed in the textbox.

pyqt line edit
Textbox with PyQt

Related course: Python Desktop Application Development with PyQt

QLineEdit Example

We start with the code followed by the explanation.

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize    
     
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
 
        self.setMinimumSize(QSize(320, 140))    
        self.setWindowTitle("PyQt Line Edit example (textfield) - pythonprogramminglanguage.com") 


        self.nameLabel = QLabel(self)
        self.nameLabel.setText('Name:')
        self.line = QLineEdit(self)
        
        self.line.move(80, 20)
        self.line.resize(200, 32)
        self.nameLabel.move(20, 20)
        
        pybutton = QPushButton('OK', self)
        pybutton.clicked.connect(self.clickMethod)
        pybutton.resize(200,32)
        pybutton.move(80, 60)        
        
    def clickMethod(self):
        print('Your name: ' + self.line.text())

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

Start by importing the QLineEdit widget:

from PyQt5.QtWidgets import QLineEdit

We will also add a text label, to show the user what to type.
Import QLabel:

from PyQt5.QtWidgets import QLabel

Then add both to the screen:

self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)
        
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)

The text value can be printed using:

print('Your name: ' + self.line.text())