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.
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 |
If you are new to Python PyQt, then I highly recommend this book.
