繁体   English   中英

PyQt5 添加滚动条到 main window

[英]PyQt5 add a scrollbar to main window

我知道已经有很多关于这个主题的话题,我试图遵循他们的建议,但我仍然很难做到这一点。

这是我的初始代码 window:

from PyQt5.QtWidgets import *
import sys

class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Python ")
        self.setGeometry(100, 100, 600, 400)
        self.UiComponents()
        self.show()

    def UiComponents(self):
        emphysemaLabel = QLabel("EMPHYSEMA", self)
        emphysemaLabel.move(10, 10)

        ggoLabel = QLabel("GGO", self)
        ggoLabel.move(10, 300)

        condensLabel = QLabel("Condens", self)
        condensLabel.move(10, 590)

        emphysema_graph_lin = QLabel(self)
        emphysema_graph_lin.resize(302, 232)
        emphysema_graph_lin.move(10, 50)
        emphysema_graph_lin.setStyleSheet("background-color:yellow;")

        ggo_graph_lin = QLabel(self)
        ggo_graph_lin.resize(302, 232)
        ggo_graph_lin.move(10, 340)
        ggo_graph_lin.setStyleSheet("background-color:yellow;")

        condens_graph_lin = QLabel(self)
        condens_graph_lin.resize(302, 232)
        condens_graph_lin.move(10, 630)
        condens_graph_lin.setStyleSheet("background-color:yellow;")

if __name__ == "__main__": 
    App = QApplication(sys.argv)
    window = Window()
    sys.exit(App.exec())

一个例子,如果我发现有用和有效的是在这里找到的代码https://www.pythonguis.com/tutorials/qscrollarea/

我试图将它应用到我的代码中,如下所示:

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
import sys


class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Python ")
        self.setGeometry(100, 100, 600, 400)
        self.UiComponents()
        self.show()

    def UiComponents(self):
        self.scroll = QScrollArea()  # Scroll Area which contains the widgets, set as the centralWidget
        self.widget = QWidget()  # Widget that contains the collection of Vertical Box
        self.vbox = QVBoxLayout()  # The Vertical Box that contains the Horizontal Boxes of  labels and buttons

        emphysemaLabel = QLabel("EMPHYSEMA", self)
        emphysemaLabel.move(10, 10)
        self.vbox.addWidget(emphysemaLabel)

        ggoLabel = QLabel("GGO", self)
        ggoLabel.move(10, 300)
        self.vbox.addWidget(ggoLabel)

        condensLabel = QLabel("Condens", self)
        condensLabel.move(10, 590)
        self.vbox.addWidget(condensLabel)

        emphysema_graph_lin = QLabel(self)
        emphysema_graph_lin.resize(302, 232)
        emphysema_graph_lin.move(10, 50)
        emphysema_graph_lin.setStyleSheet("background-color:yellow;")
        self.vbox.addWidget(emphysema_graph_lin)

        ggo_graph_lin = QLabel(self)
        ggo_graph_lin.resize(302, 232)
        ggo_graph_lin.move(10, 340)
        ggo_graph_lin.setStyleSheet("background-color:yellow;")
        self.vbox.addWidget(ggo_graph_lin)

        condens_graph_lin = QLabel(self)
        condens_graph_lin.resize(302, 232)
        condens_graph_lin.move(10, 630)
        condens_graph_lin.setStyleSheet("background-color:yellow;")
        self.vbox.addWidget(condens_graph_lin)

        self.widget.setLayout(self.vbox)

        # Scroll Area Properties
        self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.scroll.setWidgetResizable(True)
        self.scroll.setWidget(self.widget)

        self.setCentralWidget(self.scroll)


if __name__ == "__main__":
    App = QApplication(sys.argv)
    window = Window()
    sys.exit(App.exec())

但它不起作用,我该怎么办? 谢谢你的任何建议。

您可以使用 QVBoxLayout object 的addStretch()方法在布局的末尾添加垂直间隔。并调整最大尺寸以查看滚动效果, setMaximumSize()

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
import sys

class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Python ")
        self.setGeometry(100, 100, 600, 400)
        
        # set maximum size of the QMainWindow
        self.setMaximumSize(600, 100)
        self.UiComponents()
        self.show()

    def UiComponents(self):
        self.scroll = QScrollArea()  # Scroll Area which contains the widgets, set as the centralWidget
        self.widget = QWidget()  # Widget that contains the collection of Vertical Box
        self.vbox = QVBoxLayout()  # The Vertical Box that contains the Horizontal Boxes of  labels and buttons

        emphysemaLabel = QLabel("EMPHYSEMA", self)
        emphysemaLabel.move(10, 10)
        self.vbox.addWidget(emphysemaLabel)

        ggoLabel = QLabel("GGO", self)
        ggoLabel.move(10, 300)
        self.vbox.addWidget(ggoLabel)

        condensLabel = QLabel("Condens", self)
        condensLabel.move(10, 590)
        self.vbox.addWidget(condensLabel)

        emphysema_graph_lin = QLabel(self)
        emphysema_graph_lin.resize(302, 232)
        emphysema_graph_lin.move(10, 50)
        emphysema_graph_lin.setStyleSheet("background-color:yellow;")
        self.vbox.addWidget(emphysema_graph_lin)

        ggo_graph_lin = QLabel(self)
        ggo_graph_lin.resize(302, 232)
        ggo_graph_lin.move(10, 340)
        ggo_graph_lin.setStyleSheet("background-color:yellow;")
        self.vbox.addWidget(ggo_graph_lin)

        condens_graph_lin = QLabel(self)
        condens_graph_lin.resize(302, 232)
        condens_graph_lin.move(10, 630)
        condens_graph_lin.setStyleSheet("background-color:yellow;")
        self.vbox.addWidget(condens_graph_lin)
        
        #  add a vertical spacer with addStretch() method
        self.vbox.addStretch()

        self.widget.setLayout(self.vbox)

        # Scroll Area Properties
        self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.scroll.setWidgetResizable(True)
        self.scroll.setWidget(self.widget)

        self.setCentralWidget(self.scroll)


if __name__ == "__main__":
    App = QApplication(sys.argv)
    window = Window()
    sys.exit(App.exec())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM