本文代码来自zetcode.com
Layout management in PyQt5
布局管理,控件可以绝对定位或者用布局类来定位。
Absolute positioning
程序员需要清楚每一个控件的位置和大小,当你使用绝对定位,需要了解以下几点限制:
1.控件大小和位置不随窗口的大小改变
2.应用在不同平台也许看起来不同
3.改变应用字体也许会影响布局
4.如果我们决定改变布局,就必需重新布局,这将十分乏味且耗时。
#!/usr/bin/python3
"""
This example shows three labels on a window
using absolute positioning.
"""
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
lbl1 = QLabel('Zetcode', self)
lbl1.move(15, 10)
lbl2 = QLabel('tutorials', self)
lbl2.move(35, 40)
lbl3 = QLabel('for programmers', self)
lbl3.move(55, 70)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Absolute')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
我们使用move()方法定位控件,上述代码中,我们使用(x,y)坐标定位。
坐标轴的初始位置在左上角,x值从左至右增大,y值从上到下增大。
Box layout
QHBoxLayout和QVBoxLayout是基本的布局类,控制控件水平和垂直。
假设我们想要将两个按钮放在右下角,我们需要使用horizontal box 和 vertical box,为了创造必要的空间,我们加入了stretch factor。
#!/usr/bin/pyton3
"""
In this example, we position two push buttons
in the bottom-right corner of the window.
"""
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QHBoxLayout, QVBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
okButton = QPushButton("OK")
cancelButton = QPushButton("Cancel")
hbox = QHBoxLayout()
hbox.addStretch(1) # 推向窗口右方
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
vbox = QVBoxLayout()
vbox.addStretch(1) # 推向窗口下方
vbox.addLayout(hbox)
self.setLayout(vbox)
self.setWindowTitle('Buttons')
self.setGeometry(300, 300, 300, 150)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
上述代码输出如下:
QGridLayout
QGridLayout是最普遍的布局类,它将空间分为行和列。
以下代码构建了一个计算器布局:
#!/usr/bin/python3
"""
In this example, we create a skeleton
of a calculator using QGridLayout.
"""
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QGridLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
names = ['Cls', 'Bck', '', 'Close',
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+']
positions = [(i, j) for i in range(5) for j in range(4)]
for position, name in zip(positions, names):
if name == '':
continue
button = QPushButton(name)
grid.addWidget(button, *position)
self.move(300, 150)
self.setWindowTitle('Calculator')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
addWidget()用来在布局中添加和创建按钮
Review example
控件可以跨越多列/行
#!/usr/bin/python3
"""
This program will create a bit more complicated
window layout using the QGridLayout manager.
"""
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QLabel, QLineEdit, QTextEdit
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
title = QLabel('Title')
author = QLabel('Author')
review = QLabel('Review')
titleEdit = QLineEdit()
authorEdit = QLineEdit()
reviewEdit = QLineEdit()
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(title, 1, 0)
grid.addWidget(titleEdit, 1,1)
grid.addWidget(author, 2, 0)
grid.addWidget(authorEdit, 2, 1)
grid.addWidget(review, 3, 0)
grid.addWidget(reviewEdit, 3, 1, 5, 1)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Review')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
上述代码创建如下:
grid.addWidget(reviewEdit, 3, 1, 5, 1)创建了reviewEdit控件跨越了5行。