PyQt实践教学(二)--Layout management in PyQt5

之前的基础教学算是过了,你可能注意到了,当你再次向你的Widget里面添加按钮的时候,你发现它并没有出现在你想要的位置,任何和界面相关的东西都是想通的,前端里面这个东西是div+css,Qt里面这个东西就是布局管理器(Layout management)了
直接看实例把

Absolute positioning(绝对定位)

# -*- coding: utf-8 -*-
"""绝对定位演示"""
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication

class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        QLabel('XzAmrzs', self).move(15,15)
        QLabel('tutorials', self).move(35, 40)
        QLabel('for programmers', self).move(55, 70)

        self.setGeometry(500, 300, 250, 150)

        self.setWindowTitle("绝对定位演示程序")
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = Example()
    sys.exit(app.exec_())
  1. We use the move() method to position our widgets. In our case these are labels. We position them by providing the x and 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.

Box layout (盒布局)

# -*- coding: utf-8 -*-
"""Box定位演示"""
import sys
from PyQt5.QtWidgets import QWidget,QPushButton,QHBoxLayout,QVBoxLayout,QApplicationclass 
Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()
    def initUI(self):

        ok_button = QPushButton("确定")
        cancel_button = QPushButton("取消")

        h_box = QHBoxLayout()
        h_box.addStretch(1)
        h_box.addWidget(ok_button)
        h_box.addWidget(cancel_button)

        v_box = QVBoxLayout() 
        v_box.addStretch(1)
        v_box.addLayout(h_box)

        self.setLayout(v_box) 

        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle("Box定位演示程序")
        self.show()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    box_layout = Example()
    sys.exit(app.exec_())
  1. We create a horizontal box layout and add a stretch factor and both buttons. The stretch adds a stretchable space before the two buttons. This will push them to the right of the window.
  2. 重要,设置窗口的主布局.To create the necessary layout, we put a horizontal layout into a vertical one. The stretch factor in the vertical box will push the horizontal box with the buttons to the bottom of the window

QGridLayout (网格布局)

#!/usr/bin/python3
# coding=utf-8
"""网格布局示例"""
import sys
from PyQt5.QtWidgets import (QWidget,QGridLayout,QPushButton,QApplication)
class GridLayout(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        grid = QGridLayout()
        self.setLayout(grid)
        buttton_names = ['Cls', 'Bck', '', 'Close',
                         '7', '8', '9', '/',
                         '4', '5', '6', '*',
                         '1', '2', '3', '-',
                         '0', '.', '=', '+']
        positions = [(i, j) for i in range(5) for j in range(4)]

        for position, name in zip(positions,buttton_names):
            if name == "":
                continue
            button = QPushButton(name)
            grid.addWidget(button, *position)

        self.move(300,150)
        self.setWindowTitle("计算器-网格布局演示程序")
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    grid_layout = GridLayout()
    sys.exit(app.exec_())
  1. Buttons are created and added to the layout with the addWidget() method.

Review example(表格布局横跨展示)

# -*- coding: utf-8 -*-
"""网格布局跨行示例"""
import sys
from PyQt5.QtWidgets import QWidget,QGridLayout,QLabel,QLineEdit,QTextEdit,QApplication
class GridLayout(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        grid = QGridLayout()
        # 表格间隔
        grid.setSpacing(20)
        grid.addWidget(QLabel("标题:"), 1, 0)
        grid.addWidget(QLineEdit(), 1, 1)
        grid.addWidget(QLabel("作者:"), 2, 0)
        grid.addWidget(QLineEdit(), 2, 1)
        grid.addWidget(QLabel("评论:"), 3, 0)
        grid.addWidget(QTextEdit(), 3, 1, 5, 1)

        self.setLayout(grid)

        self.setGeometry(300, 300, 350, 350)
        self.setWindowTitle("网格布局跨行演示程序")
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    grid_layout = GridLayout()
    sys.exit(app.exec_())
  1. If we add a widget to a grid, we can provide row span and column span of the widget. In our case, we make the reviewEdit widget span 5 rows.

总结:

布局大概就这些了,整体思想就是:

  1. 避免使用绝对位置的布局方式
  2. 使用layout.addWidget(a_widget)
  3. 每个窗体都要有一个总布局,总布局里面套小布局a_layout.addLayout(b_layout)
  4. 对于盒布局,善于使用 addStretch(1)

你可能感兴趣的:(PyQt实践教学(二)--Layout management in PyQt5)