📜  PyQt5 - 可滚动标签

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

PyQt5 - 可滚动标签

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

滚动标签类语法

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)

        # setting alignment to the text
        self.label.setAlignment(Qt.AlignLeft | Qt.AlignTop)

        # 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)

下面是实现

Python3
# 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)
 
        # setting alignment to the text
        self.label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
 
        # 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)
 
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 " \
               "application and PyQt5 is one of them. PyQt5 is cross-platform " \
               "GUI toolkit, a set of python bindings for Qt v5. One can develop" \
               " an interactive desktop application with so much ease because " \
               "of the tools and simplicity provided by this library.A GUI application" \
               " consists of Front-end and Back-end. PyQt5 has provided a tool called " \
               "‘QtDesigner’ to design the front-end by drag and drop method so that " \
               "development can become faster and one can give more time on back-end stuff. "
 
        # creating scroll label
        label = ScrollLabel(self)
 
        # setting text to the label
        label.setText(text)
 
        # setting geometry
        label.setGeometry(100, 100, 200, 80)
 
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
window.show()
 
# start the app
sys.exit(App.exec())


输出 :