Learn Python PyQt

pyqt tooltip

We introduce the PyQt5 tooltip function in detail.

This article will teach us how to use the tooltip feature of the PyQt5 control.
A tooltip is any message that shows as a hint.

Book: Create Desktop Apps with Python PyQt5

PyQt tooltip

If you hover your mouse over the button, you will see a tooltip message.

import sys

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

class Example(QWidget):

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

  def initUI(self):
    QToolTip.setFont(QFont('Arial', 14))
    self.setToolTip('Tooltip for <b>QWidget</b>')
    btn = QPushButton('Button', self)
    btn.setToolTip('Tooltip for <b>QPushButton</b>')
    btn.resize(btn.sizeHint())
    btn.move(50, 50)
    self.setGeometry(300, 300, 300, 220)
    self.setWindowTitle('PyQt tooltip')    
    self.show()

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

In this example, we show tooltips for two PyQt5 controls.

QToolTip.setFont(QFont('Arial', 14))

This static method sets the font used for the tooltip, we use a 10px size with an italic font

self.setToolTip('This is a <b>QWidget</b> control')

To create a tooltip for this window control, we use the setTooltip() method, which displays text that we can use in rich text format.

btn = QPushButton('button', self)

btn.setToolTip('This is a <b>QPushButton</b> control')

We created a button control and set the tooltip for that control.

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

The sizeHint() method gives this button a recommended size.

python pyqt tooltip