python控件布局常用方法_python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例...

本文详细介绍了PyQt5的QGridLayout布局控件,包括如何创建网格、添加部件、设置行列跨越和间距。通过实例展示了如何创建按钮布局和故障申告表单,帮助读者掌握QGridLayout的使用技巧。
摘要由CSDN通过智能技术生成

PyQt5布局控件QGridLayout简介

QGridLayout(网格布局)是将窗口分割成行和列的网格来进行排列,通常可以使用函数addWidget()将被管理的控件(Widget)添加到窗口中,或者使用addLayout()函数将布局(layout)添加到窗口中,也可以通过addWIdget()函数对所添加的控件设置行数与列数的跨越,最后实现网格占据多个窗格

QGridLayout类中常用的方法

方法

描述

addWidget(QWidget Widget,int row,int col,int alignment=0)

给网格布局添加部件,设置指定的行和列,起始位置的默认值为(0,0)

widget:所添加的控件

row:控件的行数,默认从0开始

column:控件的列数,默认从0开始

alignment:对齐方式

addWidget(QWidget widget,int fromRow,int fromColulmn,int rowSpan,int columnSpan,Qt.Alignment alignment=0)

所添加的的控件跨越很多行或者列的时候,使用这个函数

widget:所添加的控件

fromRow:控件的起始行数

fronColumn:控件的起始列数

rowSpan:控件跨越的行数

column:控件跨越的列数

alignment:对齐方式

setSpacing(int spacing)

设置软件在水平和垂直方向的间隔

QGridLayout单一的网格单元格实例

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创建QGridLayout的实例,并设置窗口的布局

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 创建按钮并通过addWIdget()方法添加到布局中

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_())

运行效果图如下

第一组代码:创建QGridLayout的实例,并设置窗口的布局

第二组代码:创建按钮的标签列表

第三组代码:在网格中创建一个位置列表

第四组代码:创建按钮并通过addWIdget()方法添加到布局中

QGridLayout跨越行和列的网格单元格实例

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_())

运行效果示意图如下

代码分析

把titleLabel放在QGridLayout布局的第一行第0列

grid.addWidget(titleLabel, 1, 0)

把titleEditl放在QGridLayout布局的第一行第1列

grid.addWidget(titleEdit, 1, 1)

把contentLabel放在QGridLayout布局的第3行第0列

grid.addWidget(contentLabel, 3, 0)

把contentEdit放在QGridLayout布局的第3行第1列,跨越5行1列

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

本文主要介绍了PyQt5布局控件QGridLayout详细使用方法与实例,更多关于PyQt5布局控件知识请查看下面的相关链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值