Open In App

PyQt5 – Scrollable Label

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will see how we can create a scroll-able label, by default when we create a label all the text is in the single line and if length of text is greater than label, extra text didn’t show, although with the help of setWordWrap method we can create a multi-line label but still if the text exceeds it will not show in the label.
In order to show whole text in a small label we have to make label scroll-able, in order to do this we have to make our own scrollable label class which inherits the QScrollArea class which allow us to make label scroll-able, below is how scroll-able label looks like
 

 

In order to do this we have to do the following –
1. Create a new class which inherits QScrollArea 
2. Inside the class create vertical layout 
3. Create a label 
4. Make the label multi-line 
5. Over-ride the setText method for label 
6. Creating object of this class inside the main window class and setting text to it 
 

Scroll Label class syntax 
 

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)

Below is the implementation 
 

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


Output : 
 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads