PySide2 – Create Tooltip
In this article, we are going to learn how to create a tooltip. A tooltip is often used to specify extra information about something when the user moves the mouse pointer over an element. In order to make it, we use setToolTip() function which is defined in PySide2.QtWidgets.
Steps to create tooltip:
1. Import PySide2 Widgets and Gui modules in your code
2. Create window with title and font setting
3. Set tooltip ‘Our Main Window’ to entire window
4. Set icons in two states active and disable with tooltip as “Active Icon” and “Disable Icon” respectively.
Input Image :
Python3
# import system module import sys # import QtWidget Modules from PySide2.QtWidgets import QApplication, QWidget, QLabel, QToolTip # import QtGui modules from PySide2.QtGui import QIcon, QPixmap, QFont class Window(QWidget): def __init__( self ): super ().__init__() # set window title self .setWindowTitle( "w3wiki - ToolTip" ) # set window geometry self .setGeometry( 300 , 300 , 500 , 400 ) # set tooltip font and font type QToolTip.setFont(QFont( "Decorative" , 30 , QFont.Bold)) # set tooltip self .setToolTip( 'Our Main Window' ) def setIconModes( self ): # set icon icon1 = QIcon( "w3wiki.png" ) # set label label1 = QLabel( 'Sample' , self ) # set image in Active state pixmap1 = icon1.pixmap( 100 , 100 , QIcon.Active, QIcon.On) # set Pixmap label1.setPixmap(pixmap1) # set tooltip text label1.setToolTip( "Active Icon" ) # set icon icon2 = QIcon( "w3wiki.png" ) # set label label2 = QLabel( 'Sample' , self ) # set image in Disabled state pixmap2 = icon2.pixmap( 100 , 100 , QIcon.Disabled, QIcon.Off) # set P label2.setPixmap(pixmap2) label2.move( 100 , 0 ) label2.setToolTip( "Disable Icon" ) myApp = QApplication(sys.argv) window = Window() window.setIconModes() window.show() myApp.exec_() sys.exit( 0 ) |
Output Image:
Active State:
Disable State: