溫馨提示×

PyQt5中怎么創建一個可滾動的窗口

小億
286
2024-03-12 16:27:38
欄目: 編程語言

要在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個標簽,并在滾動時可以看到所有的標簽。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女