PyQt5 可滚动标签 - 将工具提示持续时间设置为标签部分
在本文中,我们将看到如何将工具提示持续时间设置为滚动标签的标签部分,当知道我们可以通过继承滚动类并在其中制作标签来制作可滚动标签时,但是当我们将工具提示设置为类对象时工具提示设置为整个小部件,即标签和滚动条。
为了将工具提示及其持续时间添加到标签部分,我们必须覆盖对象的函数。
Steps for implementations –
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 method and add tool tip to the label
7. Over-ride the setToolTipDuration method add duration the label
8. Create object of this class inside the main window class and setting text to it
9. Add tool tip to the object with the help of setToolTip
method
10. Add tool tip duration to the object with the help of setToolTipDuration
method.
下面是实现
# 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())
输出 :
此工具提示将在 1000 毫秒后消失