原文: http://zetcode.com/gui/pyqt5/layout/

布局管理是我们将小部件放置在应用窗口中的方式。 我们可以使用绝对定位或布局类放置小部件。 使用布局管理器管理布局是组织窗口小部件的首选方式。

绝对定位

程序员以像素为单位指定每个小部件的位置和大小。 使用绝对定位时,我们必须了解以下限制:

  • 如果我们调整窗口大小,则小部件的大小和位置不会改变
  • 在各种平台上,应用看起来可能有所不同
  • 在我们的应用中更改字体可能会破坏布局
  • 如果我们决定更改布局,则必须完全重做布局,这既繁琐又耗时

以下示例将小部件放置在绝对坐标中。

absolute.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. This example shows three labels on a window
  6. using absolute positioning.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. import sys
  12. from PyQt5.QtWidgets import QWidget, QLabel, QApplication
  13. class Example(QWidget):
  14. def __init__(self):
  15. super().__init__()
  16. self.initUI()
  17. def initUI(self):
  18. lbl1 = QLabel('Zetcode', self)
  19. lbl1.move(15, 10)
  20. lbl2 = QLabel('tutorials', self)
  21. lbl2.move(35, 40)
  22. lbl3 = QLabel('for programmers', self)
  23. lbl3.move(55, 70)
  24. self.setGeometry(300, 300, 250, 150)
  25. self.setWindowTitle('Absolute')
  26. self.show()
  27. if __name__ == '__main__':
  28. app = QApplication(sys.argv)
  29. ex = Example()
  30. sys.exit(app.exec_())

我们使用move()方法定位小部件。 在我们的例子中,这些是标签。 我们通过提供 x 和 y 坐标来定位它们。 坐标系的起点在左上角。 x 值从左到右增长。 y 值从上到下增长。

  1. lbl1 = QLabel('Zetcode', self)
  2. lbl1.move(15, 10)

标签窗口小部件位于x=15y=10处。

PyQt5 中的布局管理 - 图1

图:绝对定位

盒子布局

QHBoxLayoutQVBoxLayout是在水平和垂直方向排列小部件的基本布局类。

想象一下,我们想在右下角放置两个按钮。 为了创建这样的布局,我们使用一个水平框和一个垂直框。 为了创建必要的空间,我们添加了拉伸因子。

buttons.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this example, we position two push
  6. buttons in the bottom-right corner
  7. of the window.
  8. Author: Jan Bodnar
  9. Website: zetcode.com
  10. Last edited: August 2017
  11. """
  12. import sys
  13. from PyQt5.QtWidgets import (QWidget, QPushButton,
  14. QHBoxLayout, QVBoxLayout, QApplication)
  15. class Example(QWidget):
  16. def __init__(self):
  17. super().__init__()
  18. self.initUI()
  19. def initUI(self):
  20. okButton = QPushButton("OK")
  21. cancelButton = QPushButton("Cancel")
  22. hbox = QHBoxLayout()
  23. hbox.addStretch(1)
  24. hbox.addWidget(okButton)
  25. hbox.addWidget(cancelButton)
  26. vbox = QVBoxLayout()
  27. vbox.addStretch(1)
  28. vbox.addLayout(hbox)
  29. self.setLayout(vbox)
  30. self.setGeometry(300, 300, 300, 150)
  31. self.setWindowTitle('Buttons')
  32. self.show()
  33. if __name__ == '__main__':
  34. app = QApplication(sys.argv)
  35. ex = Example()
  36. sys.exit(app.exec_())

该示例在窗口的右下角放置了两个按钮。 当我们调整应用窗口的大小时,它们会停留在该位置。 我们同时使用HBoxLayoutQVBoxLayout

  1. okButton = QPushButton("OK")
  2. cancelButton = QPushButton("Cancel")

在这里,我们创建两个按钮。

  1. hbox = QHBoxLayout()
  2. hbox.addStretch(1)
  3. hbox.addWidget(okButton)
  4. hbox.addWidget(cancelButton)

我们创建一个水平框布局,并添加一个拉伸因子和两个按钮。 拉伸在两个按钮之前增加了可拉伸的空间。 这会将它们推到窗口的右侧。

  1. vbox = QVBoxLayout()
  2. vbox.addStretch(1)
  3. vbox.addLayout(hbox)

将水平布局放置在垂直布局中。 垂直框中的拉伸因子会将带有按钮的水平框推到窗口底部。

  1. self.setLayout(vbox)

最后,我们设置窗口的主要布局。

PyQt5 中的布局管理 - 图2

图:按钮

QGridLayout

QGridLayout是最通用的布局类。 它将空间分为行和列。

calculator.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this example, we create a skeleton
  6. of a calculator using QGridLayout.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. import sys
  12. from PyQt5.QtWidgets import (QWidget, QGridLayout,
  13. QPushButton, QApplication)
  14. class Example(QWidget):
  15. def __init__(self):
  16. super().__init__()
  17. self.initUI()
  18. def initUI(self):
  19. grid = QGridLayout()
  20. self.setLayout(grid)
  21. names = ['Cls', 'Bck', '', 'Close',
  22. '7', '8', '9', '/',
  23. '4', '5', '6', '*',
  24. '1', '2', '3', '-',
  25. '0', '.', '=', '+']
  26. positions = [(i,j) for i in range(5) for j in range(4)]
  27. for position, name in zip(positions, names):
  28. if name == '':
  29. continue
  30. button = QPushButton(name)
  31. grid.addWidget(button, *position)
  32. self.move(300, 150)
  33. self.setWindowTitle('Calculator')
  34. self.show()
  35. if __name__ == '__main__':
  36. app = QApplication(sys.argv)
  37. ex = Example()
  38. sys.exit(app.exec_())

在我们的示例中,我们创建了一个按钮网格。

  1. grid = QGridLayout()
  2. self.setLayout(grid)

创建QGridLayout的实例,并将其设置为应用窗口的布局。

  1. names = ['Cls', 'Bck', '', 'Close',
  2. '7', '8', '9', '/',
  3. '4', '5', '6', '*',
  4. '1', '2', '3', '-',
  5. '0', '.', '=', '+']

这些是稍后用于按钮的标签。

  1. positions = [(i,j) for i in range(5) for j in range(4)]

我们在网格中创建位置列表。

  1. for position, name in zip(positions, names):
  2. if name == '':
  3. continue
  4. button = QPushButton(name)
  5. grid.addWidget(button, *position)

使用addWidget()方法创建按钮并将其添加到布局。

PyQt5 中的布局管理 - 图3

图:计算机骨架

回顾示例

小部件可以跨越网格中的多个列或行。 在下一个示例中,我们将对此进行说明。

review.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this example, we create a bit
  6. more complicated window layout using
  7. the QGridLayout manager.
  8. author: Jan Bodnar
  9. website: zetcode.com
  10. last edited: January 2015
  11. """
  12. import sys
  13. from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit,
  14. QTextEdit, QGridLayout, QApplication)
  15. class Example(QWidget):
  16. def __init__(self):
  17. super().__init__()
  18. self.initUI()
  19. def initUI(self):
  20. title = QLabel('Title')
  21. author = QLabel('Author')
  22. review = QLabel('Review')
  23. titleEdit = QLineEdit()
  24. authorEdit = QLineEdit()
  25. reviewEdit = QTextEdit()
  26. grid = QGridLayout()
  27. grid.setSpacing(10)
  28. grid.addWidget(title, 1, 0)
  29. grid.addWidget(titleEdit, 1, 1)
  30. grid.addWidget(author, 2, 0)
  31. grid.addWidget(authorEdit, 2, 1)
  32. grid.addWidget(review, 3, 0)
  33. grid.addWidget(reviewEdit, 3, 1, 5, 1)
  34. self.setLayout(grid)
  35. self.setGeometry(300, 300, 350, 300)
  36. self.setWindowTitle('Review')
  37. self.show()
  38. if __name__ == '__main__':
  39. app = QApplication(sys.argv)
  40. ex = Example()
  41. sys.exit(app.exec_())

我们创建一个窗口,其中有三个标签,两个行编辑和一个文本编辑小部件。 使用QGridLayout完成布局。

  1. grid = QGridLayout()
  2. grid.setSpacing(10)

我们创建网格布局并设置小部件之间的间距。

  1. grid.addWidget(reviewEdit, 3, 1, 5, 1)

如果我们将小部件添加到网格,则可以提供小部件的行跨度和列跨度。 在我们的例子中,我们使reviewEdit小部件跨越 5 行。

PyQt5 中的布局管理 - 图4

图:回顾 example

PyQt5 教程的这一部分专门用于布局管理。