布局是 GUI程序开发中非常重要的一个环 ,而布局 理就是要 如何在窗口放置需要的部件,PyQt4中有两种方法来完成布局任 ,一个是 对位置(absolutepositioning),另一个就是使用布局类(layout class)。
一、相对位置(absolute positioning)
这种方法要求程序 在程序中指定每一个部件的 标位置和大小,注意事项:
1.指定了 标和大小的部件不能 随着窗口大小的变化而变化
2.程序在不同的操作系统平台上也许会有变化
3.改变字体可能会引起布局的 乱
4.如果需要改变当前的布局,就需要重新编 ,这意 着非常大的工作量。
下面看一个例子:
# !/usr/bin/python
import sys
from PyQt4 import QtGui
class Absolute(QtGui.QWidget):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.setWindowTitle('Communication')
label = QtGui.QLabel('Could\'t',self)
label.move(15,10)
label = QtGui.QLabel('care',self)
label.move(35,40)
label = QtGui.QLabel('less',self)
label.move(55,65)
label = QtGui.QLabel('And', self)
label.move(115, 65)
label = QtGui.QLabel('then', self)
label.move(135, 45)
label = QtGui.QLabel('you', self)
label.move(115, 25)
label = QtGui.QLabel('kissed', self)
label.move(145, 10)
label = QtGui.QLabel('me', self)
label.move(215, 10)
self.resize(400, 150)
app = QtGui.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
qb = Absolute()
qb.show()
sys.exit(app.exec_())
运行结果如下:
在这里就是简单的调用 move()方法来指定部件的放置坐标,坐标的原点就是窗口的左上角,x由左向右不断增大,y由上到下不断增大。
二、使用布局类
1.首先看 Box Layout
最基本的布局类是 QHBoxLayout和 VHBoxLayout,它们将部件线性水平或垂直 列。这里假设我们要 两个按钮放在窗口的右下角,要实现这样的布局,使用一个QHBoxLayout和一个 QVBoxLayout,而其他的空间,通过添加 stretch factor来实现。
1.首先看 Box Layout
最基本的布局类是 QHBoxLayout和 VHBoxLayout,它们将部件线性水平或垂直 列。这里假设我们要 两个按钮放在窗口的右下角,要实现这样的布局,使用一个QHBoxLayout和一个 QVBoxLayout,而其他的空间,通过添加 stretch factor来实现。
# !/usr/bin/python
import sys
from PyQt4 import QtGui
class BoxLayout(QtGui.QWidget):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.setWindowTitle('boxlayout')
#创建两个按钮部件
ok = QtGui.QPushButton('OK')
cancel = QtGui.QPushButton('Cancel')
#创建水平排列布局
hbox = QtGui.QHBoxLayout()
#增加伸缩量(一共九个)
hbox.addStretch(1)
hbox.addWidget(ok)
hbox.addWidget(cancel)
#创建一个垂直排列布局
vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.resize(300,150)
app = QtGui.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
qb = BoxLayout()
qb.show()
sys.exit(app.exec_())
运行结果如下: 最常用的布局类是 QGridLayout,他将窗口分为不同的行和列
# !/usr/bin/python
import sys
from PyQt4 import QtGui
class GridLayout(QtGui.QWidget):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.setWindowTitle('grid layout')
names = ['Cls','Bck','Close','7','8','9','/',
'4','5','6','#','1','2','3','-','0',
'.','=','+']
#创建一个网格布局
grid = QtGui.QGridLayout()
j = 0
pos = [
(0,0),(0,1),(0,2),(0,3),
(1,0),(1,1),(1,2),(1,3),
(2,0),(2,1),(2,2),(2,3),
(3,0),(3,1),(3,2),(3,3),
(4,0),(4,1),(4,2),(4,3)]
for i in names:
button = QtGui.QPushButton(i)
if j == 2:
grid.addWidget(QtGui.QLabel(''),0,2)
else:
grid.addWidget(button,pos[j][0],pos[j][1])
j = j+1
self.setLayout(grid)
app = QtGui.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
qb = GridLayout()
qb.show()
sys.exit(app.exec_())
运行结果:
当然一个部件也可以占多行多列,如下面的例子:
# !/usr/bin/python
import sys
from PyQt4 import QtGui
class GridLayout2(QtGui.QWidget):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.setWindowTitle('grid layout2')
title = QtGui.QLabel('Title')
author = QtGui.QLabel('Author')
review = QtGui.QLabel('Review')
titleEdit = QtGui.QLineEdit()
authorEdit = QtGui.QLineEdit()
reviewEdit = QtGui.QTextEdit()
grid = QtGui.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)
#设置这个部件放在第三行,第一列,后面5跟1分别表示占5行跟1列
grid.addWidget(reviewEdit,3,1,5,1)
self.setLayout(grid)
self.resize(500,300)
app = QtGui.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
qb = GridLayout2()
qb.show()
sys.exit(app.exec_())
运行结果: 其中 grid.setSpacing(10)是设定部件之间的距离是10个 。
grid.addWidget(reviewEdit,3,1,5,1)这个部件放到了第三行,第一列,并且后面的5和1,分别代表所占的行数和列数,就是表示放到第三行,大小占5行。
有了上面的讲解,布局管理基本上就能实现合理。