PyQt tooltip

A tooltip is a word-in-the-air help that describes the functionality of a widget. (See QToolTip official documentation) You can have tooltips appear for all components in the widget. Now let's use the setToolTip() method to create a tooltip for the widget.

Related course: Create Desktop Apps with Python PyQt5

pyqt tooltip

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QToolTip
from PyQt5.QtGui import QFont

class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        QToolTip.setFont(QFont('SansSerif', 10))
        self.setToolTip('This is a <b>QWidget</b> widget')

        btn = QPushButton('Button', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.move(50, 50)
        btn.resize(btn.sizeHint())

        self.setWindowTitle('Tooltips')
        self.setGeometry(300, 300, 300, 200)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

This example shows the tooltips for two PyQt5 widgets. When you hover over the pushbutton (btn) and MyApp widgets, the text you set appears as a tooltip, respectively.

pyqt tooltip

Related course: Create Desktop Apps with Python PyQt5

Description

QToolTip.setFont(QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')

First, set the font to be used for the tooltip. Here we use a 10px 'SansSerif' font. To create a tooltip, use the setToolTip() method to enter the text that will be displayed.

btn = QPushButton('Button', self)
btn.setToolTip('This is a <b>QPushButton</b> widget')

Create one pushbutton and attach the tooltip to it.

btn.move(50, 50)
btn.resize(btn.sizeHint())

Set the position and size of the button. The sizeHint() method helps you set the button to the appropriate size.