当前位置:网站首页>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_())
边栏推荐
- Quaternion and its simple application in unity
- Unity3d learning notes (I)
- dataframe. to_ Sql() inserts too many errors at one time
- Evaluation index of machine learning classification model and implementation of sklearn code
- What are the backup and recovery methods of gbase 8s database
- Safety is no longer the only selling point. Test drive "slash youth" Volvo C40
- 基于C语言模拟实现DFA识别字符串
- On contract testing
- Notes on network principles (five layer network)
- 使用cpolar发布树莓派网页(cpolar功能的完善)
猜你喜欢
Evaluation index of machine learning classification model and implementation of sklearn code
浅谈契约测试
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
Introduction to translation professional qualification (level) examination
dataframe. to_ Sql() inserts too many errors at one time
AxureRP原型设计 快速开始
C # use database to bind listview control data
Database system design: partition
[unity entry program] C # and unity - understand classes and objects
36. JS animation
随机推荐
How to change MySQL into Chinese
(Video + graphics) introduction to machine learning series - Chapter 1 Introduction
Summary of pit trampling records and solutions of data warehouse project
高智伟:数据管理赋能交通行业数字化转型
On the charm of code language
Webassembly 2022 questionnaire results are fresh
浅谈契约测试
不用Swagger,那我用啥?
Compile and install Apache for rocky Foundation
36. JS animation
AxureRP原型设计 快速开始
STM32 application development practice tutorial: design and implementation of controllable LED water lamp
View port occupancy
Floweable advanced
Summary of research on endogenous information security technology of industrial measurement and control equipment
No swagger, what do I use?
Gutcloud technology restcloud completed the pre-A round of financing of tens of millions of yuan
[Bert multi label text classification practice] I - overview of practical projects
多标签用户画像分析跑得快的关键在哪里?
Could not receive a message from the daemon