A tooltip is a hint in a desktop application (graphical user interface). Tooltips frequently appear when you hover the mouse over a widget (without clicking). If you put the mouse on top of a button it often shows information, that information is a tooltip message.

Pyqt supports tool tips, they can be configured for widgets. In PyQt a tooltip can contain both a plain text message and an HTML formatted message.

Related course: Create PyQt Desktop Appications with Python (GUI)

Pyqt5 tooltip

Note: Tooltips are not shown on mobile devices because there’s no mouse cursor.

A tooltip can be set using a widgets setTooltip method. This method is called on the created widget.

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

The setToolTip() method sets the tooltip message for a QtWidget. To show the tooltip, you must put the mouse on top of the widget and wait a few seconds.

pyqt tooltip

The tooltip text may contain HTML (the format used to define web pages). This format uses html tags <tag></tag> to format tags.

button.setToolTip('This is a <b>QPushButton</b> widget')

You can also change the font type of the tooltip:

QToolTip.setFont(QFont('SansSerif', 10))

Tooltip example

The example creates a button in a PyQt window. The button is linked to a callback, meaning you click on it, it will call the clickMethod() function.

The button has a tooltip message that is shown when hovered, this is set with the method setToolTip().

Any widget can have a tooltip message set. Usually tooltips are set to create helpful assistance to the user.

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

If you are new to Python PyQt, then I highly recommend this book.

Download PyQt Examples