📌  相关文章
📜  PyQt5 可滚动标签 - 设置工具提示

📅  最后修改于: 2022-05-13 01:55:38.259000             🧑  作者: Mango

PyQt5 可滚动标签 - 设置工具提示

在本文中,我们将看到如何将工具提示设置为可滚动标签。默认情况下,当我们创建标签时,所有文本都在单行中,如果文本长度大于标签,则不会显示额外的文本,尽管借助setWordWrap方法我们可以创建多行标签,但如果文本超出它不会显示在标签中。为了在一个小标签中显示整个文本,我们必须使标签可滚动,为此我们必须创建自己的可滚动标签类,它继承了 QScrollArea 类,它允许我们使标签可滚动。

下面是实现

# 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
  
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")
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :