当前位置:网站首页>PyQt5快速开发与实战 6.5 QGridLayout(网格布局)
PyQt5快速开发与实战 6.5 QGridLayout(网格布局)
2022-07-29 09:25:00 【Ding Jiaxiong】
PyQt5快速开发与实战
6. 第6章 PyQt5 布局管理
6.5 QGridLayout(网格布局)
QGridLayout(网格布局)是将窗口分隔成行和列的网格来进行排列。
通常可以使用函数addWidget()将被管理的控件(Widget)添加到窗口中,或者使用addLayout()函数将布局(Layout)添加到窗口中。也可以通过addWidget()函数对所添加的控件设置行数和列数的跨越,最后实现网格占据多个窗格。
QGridLayout类中的常用方法:
| 方法 | 描述 |
|---|---|
| addWidget( QWidget widget,int row, int col,int alignment= 0) | 给网格布局添加控件,设置指定的行和列。起始位置(top-left position)的默认值是(0,0) ;widget:所添加的控件; |
| row:控件的行数,默认从0开始;column:控件的列数,默认从0开始;alignment:对齐方式 | |
| addWidget(QWidget widget,int fromRow,int fromColumn, int rowSpan, int columnSpan,Qt.Alignment alignment= 0) | 所添加的控件跨越很多行或者列时,使用这个函数。widget:所添加的控件;fromRow:控件的起始行数;fromColumn:控件的起始列数;rowSpan:控件跨越的行数;columnSpan:控件跨越的列数;alignment:对齐方式 |
| setSpacing(int spacing) | 设置控件在水平和垂直方向的间隔 |
QGridLayout类的继承结构:
QObject → QLayout → QGridLayout
6.5.1 单一的网格单元格
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_())

6.5.2 跨越行和列的网格单元格
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) ## 把titleLabel放在第1行第0列
grid.addWidget(titleEdit, 1, 1) # 把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) # 放在第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_())

边栏推荐
- Could not receive a message from the daemon
- 23考研人撑住!考研第一波弃考高峰期已经到来!
- [Bert multi label text classification practice] I - overview of practical projects
- 【BERT-多标签文本分类实战】之一——实战项目总览
- mysql将部分表名统一转换为大写
- Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
- Trie树(字典树)讲解
- VS2015采用loadlibrary方式调用dll库
- Safety is no longer the only selling point. Test drive "slash youth" Volvo C40
- dataframe.to_sql() 一次性插入过多报错
猜你喜欢

On the charm of code language

Introduction to translation professional qualification (level) examination

【机器学习】逻辑回归代码练习
![[machine learning] logistic regression code exercise](/img/dc/203f240e2eb213dbd6173d17e47ec6.jpg)
[machine learning] logistic regression code exercise

No swagger, what do I use?

Flowable 高级篇

高智伟:数据管理赋能交通行业数字化转型

dataframe.to_sql() 一次性插入过多报错

(视频+图文)机器学习入门系列-第1章 引言

Will the modified data be updated when it is the same as the original data?
随机推荐
Floweable foundation Chapter 1
QMainWindow 详解
如何为OpenHarmony做贡献
Tesseract图文识别--简单
How to realize the isolation level between MySQL transactions and mvcc
Sublime text create page
Quaternion and its simple application in unity
Webassembly 2022 questionnaire results are fresh
Safety is no longer the only selling point. Test drive "slash youth" Volvo C40
[performance optimization methodology series] III. core idea of performance optimization (2)
Using logistic regression and neural network to deal with complex binary classification problems
附录2-一些简单的练习
dataframe. to_ Sql() inserts too many errors at one time
【Unity入门计划】C#与Unity-了解类和对象
Study and exploration of Redux API implementation of Redux
STM32 application development practice tutorial: design and implementation of controllable LED water lamp
Use cpolar to publish raspberry pie web pages (improvement of cpolar tunnel)
基于ArkUI eTS开发的坚果新闻(NutNews)
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
数据表示与计算(进制)