python pyqt5 QGridLayout网格布局

-- coding: utf-8 --

"""
【简介】
网格布局管理例子

"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton

class Winform(QWidget):
def init(self, parent=None):
super(Winform, self).init(parent)
self.initUI()

def initUI(self):
    # 1
    grid = QGridLayout()
    self.setLayout(grid)

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

    # 3
    positions = [(i, j) for i in range(5) for j in range(4)]

    # 4
    for position, name in zip(positions, names):
        if name == '':
            continue

        button = QPushButton(name)
        grid.addWidget(button, *position)

    self.move(300, 150)
    self.setWindowTitle('网格布局管理例子')

if name == "main":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())


-- coding: utf-8 --

"""
【简介】
网格布局管理例子

"""

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

class Winform(QWidget):
def init(self, parent=None):
super(Winform, self).init(parent)
self.initUI()

def initUI(self):
    titleLabel = QLabel('标题')
    authorLabel = QLabel('提交人')
    contentLabel = QLabel('申告内容')

    titleEdit = QLineEdit()
    authorEdit = QLineEdit()
    contentEdit = QTextEdit()

    grid = QGridLayout()
    grid.setSpacing(10)

    grid.addWidget(titleLabel, 1, 0)
    grid.addWidget(titleEdit, 1, 1)

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

    grid.addWidget(contentLabel, 3, 0)
    grid.addWidget(contentEdit, 3, 1, 5, 1)

    self.setLayout(grid)

    self.setGeometry(300, 300, 350, 300)
    self.setWindowTitle('故障申告')

if name == "main":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())

你可能感兴趣的:(python pyqt5 QGridLayout网格布局)