PyQt5でスクロール可能なウィンドウを作成する方法は何ですか?
PyQt5でスクロール可能なウィンドウを作成するには、QScrollAreaクラスを使用することができます。以下に簡単なサンプルコードを示します:
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_())
この例では、QWidgetウィンドウを作成し、そのウィンドウ内でQScrollAreaを使用しました。QScrollAreaに50個のラベルを追加し、スクロールするとすべてのラベルが表示されます。