PyQt【第三篇】 布局管理

绝对定位

程序指定了组件的位置并且每个组件的大小用像素作为单位来丈量。当你使用了绝对定位,我们需要知道下面的几点限制:

  • 如果我们改变了窗口大小,组件的位置和大小并不会发生改变。
  • 在不同平台上,应用的外观可能不同
  • 改变我们应用中的字体的话可能会把应用弄得一团糟。
  • 如果我们决定改变我们的布局,我们必须完全重写我们的布局,这样非常乏味和浪费时间。

import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication


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

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 400, 300)
        self.setWindowTitle('Absolute')
        self.initLayout()
        self.show()

    def initLayout(self):
        lbl1 = QLabel('label1', self)
        lbl1.move(15, 10)

        lbl2 = QLabel('label2', self)
        lbl2.move(35, 40)

        lbl3 = QLabel('label3', self)
        lbl3.move(55, 70)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

箱布局

布局管理器的布局管理类非常灵活,实用。它是将组件定位在窗口上的首选方式。QHBoxLayout和QVBoxLayout是两个基础布局管理类,他们水平或垂直的线性排列组件。

import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,
                             QHBoxLayout, QVBoxLayout, QApplication)


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

    def initUI(self):
        self.setGeometry(300, 300, 400, 300)
        self.setWindowTitle('Buttons')
        self.initLayout()
        self.show()

    def initLayout(self):
        # 创建两个按钮
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")

        # 创建一个水平箱布局
        hbox = QHBoxLayout()
        hbox.addStretch(1)  # 水平箱增加一个拉伸因子
        hbox.addWidget(okButton)  # 水平箱增加两个按钮
        hbox.addWidget(cancelButton)

        # 创建一个垂直箱布局
        vbox = QVBoxLayout()
        vbox.addStretch(1)  # 垂直箱增加一个拉伸因子
        vbox.addLayout(hbox)  # 垂直箱增加水平箱

        self.setLayout(vbox)  # 设置窗口的主布局


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

网格布局

最通用的布局类别是网格布局(QGridLayout)。该布局方式将窗口空间划分为许多行和列。
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout,
                             QPushButton, QApplication)


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

    def initUI(self):
        self.move(400, 300)
        self.setWindowTitle('Calculator')
        self.initLayout()
        self.show()

    def initLayout(self):
        # 创建一个网格布局
        grid = QGridLayout()
        self.setLayout(grid)

        # 创建按钮名称
        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, names):  # 同时遍历两个列表
            print(position, name)
            if name == '':
                continue
            button = QPushButton(name)  # 创建按钮
            grid.addWidget(button, *position)  # 网格布局增加按钮


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit,
                             QTextEdit, QGridLayout, QApplication)


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

        self.initUI()

    def initUI(self):
        self.move(400, 300)
        self.setWindowTitle('Calculator')
        self.initLayout()
        self.show()

    def initLayout(self):
        # 创建三个标签
        title = QLabel('Title')
        author = QLabel('Author')
        review = QLabel('Review')

        # 创建三个文本框
        titleEdit = QLineEdit()
        authorEdit = QLineEdit()
        reviewEdit = QTextEdit()

        # 创建一个网格布局
        grid = QGridLayout()
        grid.setSpacing(10) # 设置组件之间的间距

        # 把每个组件添加到对应的位置
        grid.addWidget(title, 1, 0)
        grid.addWidget(titleEdit, 1, 1)

        grid.addWidget(author, 2, 0)
        grid.addWidget(authorEdit, 2, 1)

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

        self.setLayout(grid)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

  

  

  

  

  

  

  

posted @ 2017-10-20 00:11  沐风先生  阅读(295)  评论(0编辑  收藏  举报