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

作者:jia666666 时间:2023-08-29 03:43:39 

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

运行效果图如下

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

第一组代码:创建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_())

运行效果示意图如下

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

代码分析

把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布局控件知识请查看下面的相关链接

来源:https://blog.csdn.net/jia666666/article/details/81701176

标签:python,PyQt5,布局
0
投稿

猜你喜欢

  • WPF自定义搜索框代码分享

    2023-07-18 23:31:04
  • SpringBoot+MySQL+Jpa实现对数据库的增删改查和分页详解

    2024-01-17 16:20:59
  • asp 关键词高亮显示(不区分大小写)

    2011-04-07 10:55:00
  • MySQL数据库改名的详细方法教程

    2024-01-18 11:46:27
  • 微信小程序 支付功能实现PHP实例详解

    2023-11-19 08:53:36
  • mysql 5.7.18 zip版安装配置方法图文教程(win7)

    2024-01-26 17:58:18
  • 对Python+opencv将图片生成视频的实例详解

    2022-01-03 08:42:16
  • js 采用delete实现继承示例代码

    2023-07-17 09:06:52
  • python实现微信跳一跳辅助工具步骤详解

    2023-08-02 11:11:40
  • python 中使用yagmail 发送邮件功能

    2022-10-27 11:48:36
  • 如何解决MySQL的客户端不支持鉴定协议

    2008-11-27 17:10:00
  • php通过exif_read_data函数获取图片的exif信息

    2023-10-27 09:01:44
  • python连接PostgreSQL数据库的过程详解

    2023-08-24 03:42:31
  • python初学定义函数

    2021-07-02 03:37:07
  • Golang Gin局部和全局中间件使用详解

    2023-07-10 03:03:00
  • python装饰器decorator介绍

    2021-12-18 10:56:25
  • Sql2005启用和关闭xp_cmdshell功能

    2008-09-29 15:37:00
  • MySQL查询条件中放置on和where的区别分析

    2024-01-25 00:22:51
  • mpvue跳转页面及注意事项

    2024-05-02 16:09:57
  • oracle日期分组查询的完整实例

    2023-06-26 10:14:13
  • asp之家 网络编程 m.aspxhome.com