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)
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
())
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy
&
Privacy Policy
Cookies are not collected in the GeeksforGeeks mobile applications.
Got It !
Please go through our recently updated
Improvement Guidelines
before submitting any improvements.
This article is being improved by another user right now.
You can suggest the changes for now and it will be under the article's discussion tab.
You will be notified via email once the article is available for improvement.
Thank you for your valuable feedback!
Please go through our recently updated
Improvement Guidelines
before submitting any improvements.
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.