## page was renamed from zhArticleTemplate ##language:zh #pragma section-numbers on ''' 含有章节索引的中文 文章模板 ''' ::-- [[zhuyj]] [<>] <> = Layout management in PyQt4 = '' Important thing in programming is the layout management. Layout management is the way how we place the widgets on the window. The management can be done in two ways. We can use absolute positioning or layout classes.'' . 对布局的管理是程序设计中一件重要的事情。布局管理就是我们在窗口上布置控件的方法。布局管理可以通过两种方式来完成,我们可以利用绝对位置或布局类来实现。 == layout classes 布局类化 == '''Absolute positioning绝对位置''' '' The programmer specifies the position and the size of each widget in pixels. When you use absolute positioning, you have to understand several things. '' . 程序员指定每个控件的位置和尺寸精确到像素。当你使用绝对位置时,你必须理解以下几件事情。 ''the size and the position of a widget do not change, if you resize a window'' . 当你改变一个窗口的尺寸时,控件的位置和尺寸不会改变 ''applications might look different on various platforms'' . 在不同的平台上应用程序看起来有很大的区别 ''changing fonts in your application might spoil the layout'' . 改变应用程序的字体可能会破坏布局 ''if you decide to change your layout, you must completely redo your layout, which is tedious and time consuming'' . 当你决定改变布局时,你必须完全重设计你的布局,这是烦闷并且浪费时间的 {{{ #!python #!/usr/bin/python # absolute.py 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('Couldn\'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(250, 150) app = QtGui.QApplication(sys.argv) qb = Absolute() qb.show() sys.exit(app.exec_()) }}} '' We simply call the move() method to position our widgets. In our case QLabel-s. We position them by providing the x and the y coordinates. The beginning of the coordinate system is at the left top corner. The x values grow from left to right. The y values grow from top to bottom.'' . 我们简单的调用move()方法来定位我们的控件。在QLabel-s框架下,我们通过指定x和y坐标来布置他们。坐标系统的开始点位于左上角,x值从左到右增长,y坐标值从上到下增长。 absolute {{attachment:absolute.jpg}} Figure: absolute positioning == Box Layout == '''盒子布局''' ''Layout management with layout classes is much more flexible and practical. It is the preferred way to place widgets on a window. The basic layout classes are QHBoxLayout