如何将Grid Layout添加到pyqt5代码

时间:2019-11-14 13:04:45

标签: python pyqt5 grid-layout

我想在我的5帧代码中添加网格布局, 它只有一个班级,框架的顶部应该是3个,底部应该是2个

这是代码。...

lass MainProg(QtWidgets.QMainWindow):

#def kiko(self,Theone):
def __init__(self):


    super(MainProg, self).__init__()

      self.setObjectName("MainWindow")
      self.resize(1364, 750)
      self.setStyleSheet("")

      #frames#################################3
    self.frame1 = QtWidgets.QFrame(self)
    self.frame1.setGeometry(QtCore.QRect(30, 40, 391, 291))
    self.frame1.setFrameShape(QtWidgets.QFrame.StyledPanel)
    self.frame1.setFrameShadow(QtWidgets.QFrame.Raised)

    self.frame2 = QtWidgets.QFrame(self)
    self.frame2.setGeometry(QtCore.QRect(440, 40, 391, 291))
    self.frame2.setFrameShape(QtWidgets.QFrame.StyledPanel)
    self.frame2.setFrameShadow(QtWidgets.QFrame.Raised)

    self.frame3 = QtWidgets.QFrame(self)
    self.frame3.setGeometry(QtCore.QRect(850, 40, 391, 291))
    self.frame3.setFrameShape(QtWidgets.QFrame.StyledPanel)
    self.frame3.setFrameShadow(QtWidgets.QFrame.Raised)

    self.frame4 = QtWidgets.QFrame(self)
    self.frame4.setGeometry(QtCore.QRect(30, 400, 391, 291))
    self.frame4.setFrameShape(QtWidgets.QFrame.StyledPanel)
    self.frame4.setFrameShadow(QtWidgets.QFrame.Raised)

    self.frame5 = QtWidgets.QFrame(self)
    self.frame5.setGeometry(QtCore.QRect(440, 400, 391, 291))
    self.frame5.setFrameShape(QtWidgets.QFrame.StyledPanel)
    self.frame5.setFrameShadow(QtWidgets.QFrame.Raised)

如果名称 ==“ 主要”:     导入系统

app = QtWidgets.QApplication(sys.argv)
player = MainProg()
player.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5 import QtCore, QtWidgets, QtGui

class MainProg(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainProg, self).__init__()
        self.setObjectName("MainWindow")
        self.resize(1364, 750)
#        self.setStyleSheet("")

        centralWidget = QtWidgets.QWidget()                       # +++
        self.setCentralWidget(centralWidget)                      # +++

          #frames#################################3
        self.frame1 = QtWidgets.QFrame(self)
        self.frame1.setGeometry(QtCore.QRect(30, 40, 391, 291))
        self.frame1.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame1.setFrameShadow(QtWidgets.QFrame.Raised)

        self.frame2 = QtWidgets.QFrame(self)
        self.frame2.setGeometry(QtCore.QRect(440, 40, 391, 291))
        self.frame2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame2.setFrameShadow(QtWidgets.QFrame.Raised)

        self.frame3 = QtWidgets.QFrame(self)
        self.frame3.setGeometry(QtCore.QRect(850, 40, 391, 291))
        self.frame3.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame3.setFrameShadow(QtWidgets.QFrame.Raised)

        self.frame4 = QtWidgets.QFrame(self)
        self.frame4.setGeometry(QtCore.QRect(30, 400, 391, 291))
        self.frame4.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame4.setFrameShadow(QtWidgets.QFrame.Raised)

        self.frame5 = QtWidgets.QFrame(self)
        self.frame5.setGeometry(QtCore.QRect(440, 400, 391, 291))
        self.frame5.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame5.setFrameShadow(QtWidgets.QFrame.Raised)

        layout = QtWidgets.QGridLayout(centralWidget)                # +++
        layout.addWidget(self.frame1, 0, 0, 1, 2)                    # +++
        layout.addWidget(self.frame2, 0, 2, 1, 2)                    # +++
        layout.addWidget(self.frame3, 0, 4, 1, 2)                    # +++
        layout.addWidget(self.frame4, 1, 0, 1, 3)                    # +++
        layout.addWidget(self.frame5, 1, 3, 1, 3)                    # +++


StyleSheet = '''
QFrame{
    background: rgb(150,150,250);
    opacity: 100;
}
'''

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setStyleSheet(StyleSheet)
    player = MainProg()
    player.show()
    sys.exit(app.exec_())

enter image description here