Pyqt 简明教程
PyQt - Layout Management
可以通过指定 GUI 小组件绝对坐标来将其放置在容器窗口中,坐标以像素为单位进行测量。坐标相对于 setGeometry() 方法定义的窗口维度。
A GUI widget can be placed inside the container window by specifying its absolute coordinates measured in pixels. The coordinates are relative to the dimensions of the window defined by setGeometry() method.
setGeometry() syntax
QWidget.setGeometry(xpos, ypos, width, height)
在下面的代码片段中,300 乘以 200 像素维度的主窗口显示在监视器上的位置 (10, 10)。
In the following code snippet, the top level window of 300 by 200 pixels dimensions is displayed at position (10, 10) on the monitor.
import sys
from PyQt4 import QtGui
def window():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
b = QtGui.QPushButton(w)
b.setText("Hello World!")
b.move(50,20)
w.setGeometry(10,10,300,200)
w.setWindowTitle(“PyQt”)
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
window()
在窗口中添加了一个 PushButton 小组件,并将其放置在距离窗口左上角 50 像素的右侧和 20 像素的下方。
A PushButton widget is added in the window and placed at a position 50 pixels towards right and 20 pixels below the top left position of the window.
然而,这个 Absolute Positioning 不合适,原因如下 -
This Absolute Positioning, however, is not suitable because of following reasons −
-
The position of the widget does not change even if the window is resized.
-
The appearance may not be uniform on different display devices with different resolutions.
-
Modification in the layout is difficult as it may need redesigning the entire form.
PyQt API 提供布局类,可以更优雅地管理容器内小组件的位置。相对于绝对定位,布局管理器的优点是 -
PyQt API provides layout classes for more elegant management of positioning of widgets inside the container. The advantages of Layout managers over absolute positioning are −
-
Widgets inside the window are automatically resized.
-
Ensures uniform appearance on display devices with different resolutions.
-
Adding or removing widget dynamically is possible without having to redesign.
以下是我们将在本章中依次讨论的类列表。
Here is the list of Classes which we will discuss one by one in this chapter.
Sr.No. |
Classes & Description |
1 |
QBoxLayoutQBoxLayout class lines up the widgets vertically or horizontally. Its derived classes are QVBoxLayout (for arranging widgets vertically) and QHBoxLayout (for arranging widgets horizontally). |
2 |
QGridLayoutA GridLayout class object presents with a grid of cells arranged in rows and columns. The class contains addWidget() method. Any widget can be added by specifying the number of rows and columns of the cell. |
3 |
QFormLayoutQFormLayout is a convenient way to create two column form, where each row consists of an input field associated with a label. As a convention, the left column contains the label and the right column contains an input field. |