PyQT5:水平布局内的网格布局

时间:2017-09-21 06:54:52

标签: python layout pyqt5 grid-layout

有人可以帮我弄清楚如何组合布局吗? 从以下指南中偏移:{https://pythonspot.com/en/pyqt5/]

我宁愿不使用Designer,因为布局将成为几个选项卡的一部分,这些选项卡是根据指定数据文件夹中的测试和数据集的数量确定的。

例如,我想切换出蓝色按钮:

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import pyqtSlot

    class App(QDialog):

        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 layout - pythonspot.com'
            self.left = 10
            self.top = 10
            self.width = 320
            self.height = 100
            self.initUI()

        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)

            self.createHorizontalLayout()

            windowLayout = QVBoxLayout()
            windowLayout.addWidget(self.horizontalGroupBox)
            self.setLayout(windowLayout)

            self.show()

        def createHorizontalLayout(self):
            self.horizontalGroupBox = QGroupBox("What is your favorite color?")
            layout = QHBoxLayout()

            buttonBlue = QPushButton('Blue', self)
            buttonBlue.clicked.connect(self.on_click)
            layout.addWidget(buttonBlue) 

            buttonRed = QPushButton('Red', self)
            buttonRed.clicked.connect(self.on_click)
            layout.addWidget(buttonRed) 

            buttonGreen = QPushButton('Green', self)
            buttonGreen.clicked.connect(self.on_click)
            layout.addWidget(buttonGreen) 

            self.horizontalGroupBox.setLayout(layout)


        @pyqtSlot()
        def on_click(self):
            print('PyQt5 button click')

    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())

使用网格布局:

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout, QGridLayout
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import pyqtSlot

    class App(QDialog):

        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 layout - pythonspot.com'
            self.left = 10
            self.top = 10
            self.width = 320
            self.height = 100
            self.initUI()

        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)

            self.createGridLayout()

            windowLayout = QVBoxLayout()
            windowLayout.addWidget(self.horizontalGroupBox)
            self.setLayout(windowLayout)

            self.show()

        def createGridLayout(self):
    self.horizontalGroupBox = QGroupBox("Grid")
    layout = QGridLayout()
    layout.setColumnStretch(1, 4)
    layout.setColumnStretch(2, 4)

    layout.addWidget(QPushButton('1'),0,0) 
    layout.addWidget(QPushButton('2'),0,1) 
    layout.addWidget(QPushButton('3'),0,2) 
    layout.addWidget(QPushButton('4'),1,0) 
    layout.addWidget(QPushButton('5'),1,1) 
    layout.addWidget(QPushButton('6'),1,2) 
    layout.addWidget(QPushButton('7'),2,0) 
    layout.addWidget(QPushButton('8'),2,1) 
    layout.addWidget(QPushButton('9'),2,2) 

    self.horizontalGroupBox.setLayout(layout)

    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())        

1 个答案:

答案 0 :(得分:4)

您可以使用类似的方式将布局添加为另一个布局的元素,以使用addLayout()

添加窗口小部件
layout = QtWidgets.QHBoxLayout()
sublayout = QtWidgets.QGridLayout()
layout.addLayout(sublayout)