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 to 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): # contructor 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
- PyQt5 Scrollable Label – Setting tool tip duration to label part
- PyQt5 Scrollable Label - Getting tool tip text of the label part
- PyQt5 Scrollable Label - Setting tool tip to the label part
- PyQt5 Scrollable Label – Getting tool tip duration of the label part
- PyQt5 - Getting text of scrollable label
- PyQt5 Scrollable Label – Getting tool tip duration
- PyQt5 Scrollable Label - Setting tool tip
- PyQt5 Scrollable Label - Retrieving tooltip text
- PyQt5 Scrollable Label – Setting tool tip duration
- PyQt5 – How to change size of the Label | label.resize method
- PyQt5 Label – Checking if label is window type
- PyQt5 Label – Checking if label is widget type
- PyQt5 – How to hide label | label.setHidden method
- PyQt5 – How to get Label coordinates ?
- PyQt5 – How to set skin of Label ?
- PyQt5 - Add Label to StatusBar
- PyQt5 – How to add padding to a Label ?
- PyQt5 - Multi line label
- PyQt5 Label - Adding shadow
- PyQt5 – How to add image in Label background ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.