在GUI编程中,布局管理相当重要,布局管理就是我们在窗口中放置小工具的方式。布局管理有两种方式
1、absolute positioning 绝对路径
2、layout classes 布局类
一、绝对路径
开发者具体指明每个工具的大小和尺寸,当你用绝对路径时,你需要理解下列东西
1、当我们resize一个窗口的时候,工具的大小的位置不会改变
2、在不同的平台,应用看起来会不一样
3、在应用中改变字体可能破坏整个布局
4、当我们改变布局的时候,必需完全重做,这是非常无聊且耗时的
下面的代码演示来如何用绝对路径的方式在一个窗口中显示3个lable
# -*- coding: utf-8 -*- """ ZetCode PySide tutorial This example shows three labels on a window using absolute positioning. author: Jan Bodnar website: zetcode.com last edited: August 2011 """ import sys from PySide import QtGui class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): label1 = QtGui.QLabel('Zetcode', self) # 创建lable label1.move(15, 10) # 放置lable到既定位置 label2 = QtGui.QLabel('tutorials', self) label2.move(35, 40) label3 = QtGui.QLabel('for programmers', self) label3.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()二、布局管理
1、用布局类来进行布局管理相当灵活而且很实用,基本的布局类是 QtGui.QHBoxLayout 和 QtGui.QVBoxLayout ,它们按照行和列对其工具(widgets)
假设我们想在右下角放置两个button,为了实现这样一个布局,我们会用到一个水平和一个竖直框,为了创建这两个button所需要的空间,还要一个 stretch factor.
# -*- coding: utf-8 -*- """