PyQt5可滚动标签 – 为标签部分设置工具提示持续时间

PyQt5可滚动标签 – 为标签部分设置工具提示持续时间

在这篇文章中,我们将看到如何为滚动标签的标签部分设置工具提示持续时间,当我们知道我们可以通过继承滚动类并在其中制作标签来制作可滚动的标签时,但当我们为类对象设置工具提示时,工具提示被设置到整个部件上,即标签和滚动条。

为了在标签部分添加工具提示和它的持续时间,我们必须覆盖对象的功能。

实现的步骤 –

1.创建一个继承于QScrollArea的新类

2.在该类中创建垂直布局

3.创建一个标签,使其成为多行,并将其添加到布局中

5.覆盖标签的setText和text方法

6.覆盖setToolTip方法,为标签添加工具提示

7.覆盖setToolTipDuration方法,为标签添加持续时间

8.在主窗口类中创建该类的对象,并为其设置文本

9.在setToolTip方法的帮助下,为该对象添加工具提示

10.在setToolTipDuration方法的帮助下,为该对象添加工具提示的持续时间。

下面是实现方法

# 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
        self.setWidgetResizable(True)
  
        # making qwidget object
        content = QWidget(self)
        self.setWidget(content)
  
        # vertical box layout
        lay = QVBoxLayout(content)
  
        # creating label
        self.label = QLabel(content)
  
        # making label multi-line
        self.label.setWordWrap(True)
  
        # adding label to the layout
        lay.addWidget(self.label)
  
    # the setText method
    def setText(self, text):
        # setting text to the label
        self.label.setText(text)
  
    # 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
        self.label.setToolTip(p_str)
  
    # setToolTipDuration method
    def setToolTipDuration(self, p_int):
        # setting tool tip duration to the label
        self.label.setToolTipDuration(p_int)
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # 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
        label.setText(text)
  
        # setting geometry
        label.setGeometry(100, 100, 150, 80)
  
        # setting tool tip
        label.setToolTip("It is tool tip")
  
        # setting tool tip duration
        label.setToolTipDuration(1000)
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :

PyQt5可滚动标签 - 为标签部分设置工具提示持续时间

该工具提示将在1000毫秒后消失。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程