Last Updated on 2021-05-19 by Clay
In the process of designing the interface, we may need to set up a “scrollable” area in the interface so that we can maximize the display of the information we want to display in the limited layout.
I learned this dragging method on the following website: https://www.riverbankcomputing.com/static/Docs/PyQt5/index.html?highlight=qicon
If you are interested in learning PyQt5, maybe you can refer to: PyQt5 Tutorial.
How to use QScrollArea
Let’s look at the code:
# -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle('QScrollArea Test') self.setGeometry(400, 400, 400, 800) formLayout = QFormLayout() groupBox = QGroupBox() for n in range(100): label1 = QLabel('Slime_%2d' % n) label2 = QLabel() label2.setPixmap(QPixmap('s1.png')) formLayout.addRow(label1, label2) groupBox.setLayout(formLayout) scroll = QScrollArea() scroll.setWidget(groupBox) scroll.setWidgetResizable(True) layout = QVBoxLayout(self) layout.addWidget(scroll) if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() sys.exit(app.exec())
Output:
This is really scrollable.
Bless you, I was struggling to implement this for weeks. Thank you!