PyQt Demo:
https://www.cnblogs.com/zach0812/p/13121523.html
我們知道QFormLayout 是兩列的(Label 和 Feild ) ,那么如果是想要 三列,想要四列如何搞呢?
就要用到這里的網格布局了,
QGridLayout的描述:
它是繼承與QLayout 的,
QGridLayout的功能作用:
構造函數:

from PyQt5.Qt import * #剛開始學習可以這樣一下導入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QGridLayout的學習") self.resize(400,400) self.set_ui() def set_ui(self): gridLayout = QGridLayout() label1= QLabel("標簽1") label1.setStyleSheet("background-color:red;") label2= QLabel("標簽2") label2.setStyleSheet("background-color:green;") label3= QLabel("標簽3") label3.setStyleSheet("background-color:yellow;") self.setLayout(gridLayout) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
元素操作:
合並單元格,要指明,從哪個開始,跨越幾行,跨越幾列。

from PyQt5.Qt import * #剛開始學習可以這樣一下導入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QGridLayout的學習") self.resize(400,400) self.set_ui() def set_ui(self): gridLayout = QGridLayout() label1= QLabel("標簽1") label1.setStyleSheet("background-color:red;") label2= QLabel("標簽2") label2.setStyleSheet("background-color:green;") label3= QLabel("標簽3") label3.setStyleSheet("background-color:yellow;") #添加控件 gridLayout.addWidget(label1,0,0) gridLayout.addWidget(label2,0,1) # gridLayout.addWidget(label3,1,0) #合並單元格的時候要告訴它 跨越多少行 和跨越多少列 gridLayout.addWidget(label3,1,0,1,2) #從1 0開始 它占的是1行 2 列 # gridLayout.addWidget(label3,1,0,3,3) #從1 0開始 它占的是3行 3 列 #添加布局 # gridLayout.addLayout() #它的智能提示不是太好 #獲取 print(gridLayout.getItemPosition(2)) print(gridLayout.itemAtPosition(1,1).widget().text()) self.setLayout(gridLayout) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
列寬/行高 和拉伸系數:
這里指的是最小的列寬和最小的行高:

from PyQt5.Qt import * #剛開始學習可以這樣一下導入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QGridLayout的學習") self.resize(400,400) self.set_ui() def set_ui(self): gridLayout = QGridLayout() label1= QLabel("標簽1") label1.setStyleSheet("background-color:red;") label2= QLabel("標簽2") label2.setStyleSheet("background-color:green;") label3= QLabel("標簽3") label3.setStyleSheet("background-color:yellow;") #添加控件 gridLayout.addWidget(label1,0,0) gridLayout.addWidget(label2,0,1) gridLayout.addWidget(label3,1,0,1,2) #設置最小的列寬/行高 # gridLayout.setColumnMinimumWidth(0,100 ) #第0列 最小 100 # gridLayout.setRowMinimumHeight(0,100 ) #第0行 最小 100 #拉伸系數 gridLayout.setColumnStretch(0,1) #第0列 的伸縮系數為1 gridLayout.setColumnStretch(1,1) #第1列 的伸縮系數為1 gridLayout.setRowStretch(0,1) #第0行 的伸縮系數為1 gridLayout.setRowStretch(1,2) #第1行 的伸縮系數為1 self.setLayout(gridLayout) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
間距:
一個是垂直方向的間距,一個是水平方向的間距。

from PyQt5.Qt import * #剛開始學習可以這樣一下導入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QGridLayout的學習") self.resize(400,400) self.set_ui() def set_ui(self): gridLayout = QGridLayout() label1= QLabel("標簽1") label1.setStyleSheet("background-color:red;") label2= QLabel("標簽2") label2.setStyleSheet("background-color:green;") label3= QLabel("標簽3") label3.setStyleSheet("background-color:yellow;") #添加控件 gridLayout.addWidget(label1,0,0) gridLayout.addWidget(label2,0,1) gridLayout.addWidget(label3,1,0,1,2) self.setLayout(gridLayout) #間距 # print(gridLayout.spacing()) # print(gridLayout.horizontalSpacing()) # print(gridLayout.verticalSpacing()) gridLayout.setVerticalSpacing(0) gridLayout.setHorizontalSpacing(0) # 等同於上兩個 gridLayout.setSpacing(0) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
原點角:
信息獲取:

from PyQt5.Qt import * #剛開始學習可以這樣一下導入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QGridLayout的學習") self.resize(400,400) self.set_ui() def set_ui(self): gridLayout = QGridLayout() label1= QLabel("標簽1") label1.setStyleSheet("background-color:red;") label2= QLabel("標簽2") label2.setStyleSheet("background-color:green;") label3= QLabel("標簽3") label3.setStyleSheet("background-color:yellow;") #添加控件 gridLayout.addWidget(label1,0,0) gridLayout.addWidget(label2,0,1) gridLayout.addWidget(label3,1,0,1,2) #信息獲取 print(gridLayout.rowCount()) print(gridLayout.columnCount()) QRect() print(gridLayout.cellRect(1,1)) #如果沒有,可以將其放到 window.show() 后 if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() print(window.gridLayout.cellRect(1,1)) sys.exit(app.exec_())
總結:
下面是QStackedLayout:https://www.cnblogs.com/zach0812/p/11402608.html