>>返回Python系列文章目录<<
每个界面窗口只能setLayout
只能设置一个布局,但是布局可以addLayout
多个布局
布局的继承关系为:
QLayout
- QBoxLayout
- QHBoxLayout
- QVBoxLayou
- QFormLayout
- QGridLayout
QVBoxLayout类和QHBoxLayout类 | 说明 |
---|---|
self=QVBoxLayout() self=QHBoxLayout() |
创建实例 |
继承自QBoxLayout类的方法 | |
self.addWidget(widget) |
将控件放入布局widget ->控件窗口或控件 |
self.addLayout(layout) |
添加子布局layout ->布局类 |
self.addStretch(n) |
添加缩放系数 |
self.addSpacing(n) |
添加距离 |
import sys
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.move(500, 400)
self.setFixedSize(300, 300)
self.setWindowTitle('垂直布局')
vlayout = QVBoxLayout()
self.setLayout(vlayout)
btn1 = QPushButton('start')
vlayout.addWidget(btn1)
btn2 = QPushButton('close')
vlayout.addWidget(btn2)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import *
from PyQt5.Qt import *
from PyQt5.QtGui import *
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.move(500, 400)
self.setFixedSize(300, 300)
self.setWindowTitle('布局嵌套')
vlayout1 = QVBoxLayout()
vlayout2 = QVBoxLayout()
hlayout = QHBoxLayout()
hlayout.addLayout(vlayout1)
hlayout.addLayout(vlayout2)
self.setLayout(hlayout)
for idx in range(1, 3):
btn = QPushButton(str(idx))
vlayout1.addWidget(btn)
for idx in range(3, 6):
btn = QPushButton(str(idx))
vlayout2.addWidget(btn)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
QGridLayout类 | 说明 |
---|---|
self=QGridLayout() |
创建实例 |
self.addWidget(widget, row, column rowSpan, columnSpan) |
将控件放入布局widget ->控件窗口或控件row , column ->行号和列号,数字rowSpan , colunmSpan ->行跨度和列跨度,向右/向下为正 |
self.addLayout(layout, row, column rowSpan, columnSpan) |
将子布局放入布局layout ->布局类row , column ->行号和列号,数字rowSpan , colunmSpan ->行跨度和列跨度,向右/向下为正 |
import sys
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.move(500, 400)
self.setFixedSize(300, 300)
self.setWindowTitle('网格布局')
layout = QGridLayout()
self.setLayout(layout)
btn = QPushButton('1-3')
layout.addWidget(btn, 0, 0, 1, 3) # 行列跨度为负数表示向左合并
btn = QPushButton('4,7')
layout.addWidget(btn, 1, 0, 2, 1)
for x in range(1, 3):
for y in range(1, 3):
btn = QPushButton(str(3*x+y))
layout.addWidget(btn, x, y) # 行列跨度默认为1
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
QFormLayout是label-field式的表单布局,顾明思议,就是实现表单方式的布局,表单是提示用户进行交互的一种模式
主要有两列组成,第一列用于显示信息,给用户提示,一般叫做label域
第二列需要用户进行选择或输入,一般叫做field域,field域可以是控件,也可以是布局
QFormLayout类 | 说明 |
---|---|
self=QFormLayout() |
创建布局 |
self.addRow(label, field) |
增加一行表单label ->QLabel类field ->控件类或布局类 |
import sys
from PyQt5.QtWidgets import *
class MyWindow(QTabWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.move(500, 400)
self.setFixedSize(300, 300)
self.setWindowTitle('表单布局示例')
flayout = QFormLayout()
labl1 = QLabel("标签1")
lineEdit1 = QLineEdit()
labl2 = QLabel("标签2")
lineEdit2 = QLineEdit()
labl3 = QLabel("标签3")
lineEdit3 = QLineEdit()
flayout.addRow(labl1, lineEdit1)
flayout.addRow(labl2, lineEdit2)
flayout.addRow(labl3, lineEdit3)
self.setLayout(flayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
>>返回Python系列文章目录<<