Open In App

PyQt5 Scrollable Label – Getting tool tip duration of the label part

Last Updated : 23 Jan, 2023
Like Article

In this article we will see how we can get the tooltip duration of the label part of scrollable label, when know we can make scrollable label with the help of inheriting a scroll class and making label in it, but when we set tooltip to the class object tooltip is set to the whole widget i.e label and the scroll bar as well. In order to get tooltip and its duration of the label part, we have to override the function of the object.

Steps for implementation – 1. Create a new class which inherits QScrollArea 2. Inside the class create vertical layout 3. Create a label and make it multi-line and add it to the layout 5. Over-ride the setText and text method for label 6. Over-ride setToolTip and setToolTipDuration method 7. Over-ride toolTipDuration method which return duration of label tool tip 8. Create object of this class inside the main window class and setting text to it 9. Add tooltip and its duration to the object with the help of setToolTip and setToolTipDuration method 10. Get the tooltip with the help of toolTipDuration method 11. Create another label to show the duration

Below is the implementation 


# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
# class for scrollable label
class ScrollLabel(QScrollArea):
    # constructor
    def __init__(self, *args, **kwargs):
        QScrollArea.__init__(self, *args, **kwargs)
        # making widget resizable
        # making qwidget object
        content = QWidget(self)
        # vertical box layout
        lay = QVBoxLayout(content)
        # creating label
        self.label = QLabel(content)
        # making label multi-line
        # adding label to the layout
    # the setText method
    def setText(self, text):
        # setting text to the label
    # getting text method
    def text(self):
        # getting text of the label
        get_text = self.label.text()
        # return the text
        return get_text
    # setToolTip method
    def setToolTip(self, p_str):
        # setting tool tip to the label
    # setToolTipDuration method
    def setToolTipDuration(self, p_int):
        # setting tool tip duration to the label
    # getting tool tip duration
    # toolTipDuration method
    def toolTipDuration(self):
        # returning the duration of label
        return self.label.toolTipDuration()
class Window(QMainWindow):
    def __init__(self):
        # setting title
        self.setWindowTitle("Python ")
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
        # calling method
        # showing all the widgets
    # method for widgets
    def UiComponents(self):
        # text to show in label
        text = "There are so many options provided by Python to develop GUI " \
               " There are so many options provided by Python to develop GUI" \
               " There are so many options provided by Python to develop GUI"
        # creating scroll label
        label = ScrollLabel(self)
        # setting text to the label
        # setting geometry
        label.setGeometry(100, 100, 150, 80)
        # setting tool tip
        label.setToolTip("It is tool tip")
        # setting tool tip duration
        # getting duration
        duration = label.toolTipDuration()
        # creating another label to show the duration
        result = QLabel("Duration : " + str(duration), self)
        # setting geometry to the label
        result.setGeometry(150, 200, 200, 30)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads