如何将网格布局添加到我的 pyqt5 代码中
How can i add Grid Layout to my pyqt5 code
我想在我的五帧代码中添加网格布局,
它只有一个 class 并且框架应该是顶部 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)
if name == "main":
导入系统
app = QtWidgets.QApplication(sys.argv)
player = MainProg()
player.show()
sys.exit(app.exec_())
试一试:
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_())
我想在我的五帧代码中添加网格布局, 它只有一个 class 并且框架应该是顶部 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)
if name == "main": 导入系统
app = QtWidgets.QApplication(sys.argv)
player = MainProg()
player.show()
sys.exit(app.exec_())
试一试:
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_())