How to create a scrollable window in PyQt5?
To create a scrollable window in PyQt5, you can use the QScrollArea class. Below is a simple example code:
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QLabel, QScrollArea
class ScrollableWindow(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
scroll = QScrollArea()
for i in range(50):
label = QLabel(f"Label {i}")
layout.addWidget(label)
widget = QWidget()
widget.setLayout(layout)
scroll.setWidget(widget)
scroll.setWidgetResizable(True)
main_layout = QVBoxLayout()
main_layout.addWidget(scroll)
self.setLayout(main_layout)
self.setWindowTitle("Scrollable Window")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ScrollableWindow()
window.show()
sys.exit(app.exec_())
In this example, we have created a QWidget window and used a QScrollArea within the window. We have added 50 labels within the QScrollArea and can see all the labels when scrolling.