|
PyQt4的 layout 管理
在編程中一個重要的事情就是 layout 的管理。layout 管理就是在窗口中布置 widget。管理有兩種方式。我們可以使用 絕對定位 或者 layout 類 。
絕對定位
程序員可以指定每個 widget 的位置和大小。當你使用絕對定位時,你需要理解一些事情。
- 如果你改變了窗口的大小,widget 的大小和位置是不會改變的。
- 在不同平臺上應用可能看起來不太一樣。
- 在你的應用中改變字體會毀掉你的 layout。
- 如果你決定改變你的 layout,那么你必須要重新設計你的 layout,這意味著麻煩及耗時。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
This example shows three labels on a window
using absolute positioning.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lbl1 = QtGui.QLabel('Zetcode', self)
lbl1.move(15, 10)
lbl2 = QtGui.QLabel('tutorials', self)
lbl2.move(35, 40)
lbl3 = QtGui.QLabel('for programmers', self)
lbl3.move(55, 70)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Absolute')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我們簡單地調用 move() 方法來定位我們的 widget 。在我們這個情況下,這些 widget 是標簽。我們通過給定 x 和 y 坐標進行定位。坐標系統是從左上角開始。x 的值從左到右增加。y 則是從上到下。
Box Layout
通過 layout 類管理會更加靈活和實用。這是優先考慮的方法?;镜?layout 類是 QtGui.QHBoxLayout 和 QtGui.QVBoxLayout 。它們分別以水平和垂直方式對 widget 進行布局。
假設我們想把兩個按鈕放置在右下角。為了創建這樣的 layout,我們可以使用一個水平的和垂直的盒子。為了創建必要的空間,我們將會增加一個 縮放比例 。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we position two push
buttons in the bottom-right corner
of the window.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
okButton = QtGui.QPushButton("OK")
cancelButton = QtGui.QPushButton("Cancel")
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Buttons')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
這里在窗口的右下角放置了兩個按鈕。他們在窗口調整時,會保持處于那個位置。我們使用了 QtGui.HBoxLayout 和 QtGui.QVBoxLayout 。
okButton = QtGui.QPushButton("OK")
cancelButton = QtGui.QPushButton("Cancel")
此處,我們創建了兩個按鈕。
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
我們創建了一個水平的 layout 。增加了縮放比例和按鈕。這個縮放在兩個按鈕前增加了合適的縮放空白。這就使得按鈕在窗口的右邊。
vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
為了創建必要的 layout,我們把水平的 layout 放置到垂直的 layout 中。而垂直盒子中的縮放因子使得水平的盒子處于窗口的底部。
最后,我們設置了窗口的 layout 。
QtGui.QGridLayout
最通用的 layout 類是網格 layout 。這個 layout 把空間分為行和列。為了創建一個網格的 layout,我們可以使用 QtGui.QGridLayout 類。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we create a skeleton
of a calculator using a QtGui.QGridLayout.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
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)
self.move(300, 150)
self.setWindowTitle('Calculator')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在我們的例子中,我們創建了一組按鈕,并以網格放置。為了填補一個空格,我們增加了一個 QtGui.QLable 的 widget 。
grid = QtGui.QGridLayout()
這里,我們創建了一個網格 layout 。
if j == 2:
grid.addWidget(QtGui.QLable(''), 0, 2)
else: grid.addWidget(button, pos[j][0], pos[j][1])
為了把一個 widget 增加到網格中,我們調用了 addWidget() 方法。參數是 widget,行和列的數字。
再看例子
在網格中,widget 可以跨多行或多列。下面的例子我們就將介紹這個。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we create a bit
more complicated window layout using
the QtGui.QGridLayout manager.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
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)
grid.addWidget(reviewEdit, 3, 1, 5, 1)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Review')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我們創建了一個窗口,其中有三個標簽,兩個行編輯區及一個文本編輯區。layout 由 QtGui.QGridLayout 創建。
grid = QtGui.QGridLayout()
grid.setSpacing(10)
我們創建了網格 layout,并設置了 widget 間的間隔。
grid.addWidget(reviewEdit, 3, 1, 5, 1)
如果我們增加一個 widget 到網格中,我們可以提供行和列的跨度。我們使得 reviewEdit 的跨度為 5 。
這個部分我們講述了 layout 管理。
|