当前位置:网站首页>PyQt5快速开发与实战 6.4 QBoxLayout(框布局)
PyQt5快速开发与实战 6.4 QBoxLayout(框布局)
2022-07-29 09:25:00 【Ding Jiaxiong】
PyQt5快速开发与实战
文章目录
6. 第6章 PyQt5 布局管理
6.4 QBoxLayout(框布局)
采用QBoxLayout类可以在水平和垂直方向上排列控件,QHBoxLayout和QVBoxLayout类继承自 QBoxLayout类。
6.4.1 QHBoxLayout(水平布局)
采用QHBoxLayout 类,按照从左到右的顺序来添加控件。
QHBoxLayout类中的常用方法:
| 方法 | 描述 |
|---|---|
| addLayout(self , QLayout , stretch = 0 ) | 在窗口的右边添加布局,使用stretch(伸缩量)进行伸缩,伸缩量默认为0 |
| addWidget(self , QWidget , stretch , Qt.Alignment alignment) | 在布局中添加控件:stretch(伸缩量),只适用于QBoxLayout,控件和窗口会随着伸缩量的变大而增大; |
| alignment,指定对齐的方式 | |
| addSpacing(self , int) | 设置各控件的上下间距,通过该方法可以增加额外的空间 |
QHBoxLayout类的继承结构:
QObject → QLayout → QBoxLayout → QHBoxLayout
QHBoxLayout布局时用到的对齐方式参数:
| 参数 | 描述 |
|---|---|
| Qt.AlignLeft | 水平方向居左对齐 |
| Qt.AlignRight | 水平方向居右对齐 |
| Qt.AlignCenter | 水平方向居中对齐 |
| Qt.AlignJustify | 水平方向两端对齐 |
| Qt.AlignTop | 垂直方向靠上对齐 |
| Qt.AlignBottom | 垂直方向靠下对齐 |
| Qt.AlignVCenter | 垂直方向居中对齐 |
水平布局案例
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle("水平布局管理例子")
# 水平布局按照从左到右的顺序进行添加按钮部件。
hlayout = QHBoxLayout()
hlayout.addWidget(QPushButton(str(1)))
hlayout.addWidget(QPushButton(str(2)))
hlayout.addWidget(QPushButton(str(3)))
hlayout.addWidget(QPushButton(str(4)))
hlayout.addWidget(QPushButton(str(5)))
self.setLayout(hlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt5.QtCore import Qt
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle("水平布局管理例子")
self.resize(800, 200)
# 水平布局按照从左到右的顺序进行添加按钮部件。
hlayout = QHBoxLayout()
# 水平居左 垂直居上
hlayout.addWidget(QPushButton(str(1)), 0, Qt.AlignLeft | Qt.AlignTop)
hlayout.addWidget(QPushButton(str(2)), 0, Qt.AlignLeft | Qt.AlignTop)
hlayout.addWidget(QPushButton(str(3)))
# 水平居左 垂直居下
hlayout.addWidget(QPushButton(str(4)), 0, Qt.AlignLeft | Qt.AlignBottom)
hlayout.addWidget(QPushButton(str(5)), 0, Qt.AlignLeft | Qt.AlignBottom)
self.setLayout(hlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())

还可以使用setSpacing(int)设置各控件之间的间距。

6.4.2 QVBoxLayout(垂直布局)
采用QVBoxLayout类,按照从上到下的顺序添加控件。
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle("垂直布局管理例子")
self.resize(330, 150)
# 垂直布局按照从上到下的顺序进行添加按钮部件。
vlayout = QVBoxLayout()
vlayout.addWidget(QPushButton(str(1)))
vlayout.addWidget(QPushButton(str(2)))
vlayout.addWidget(QPushButton(str(3)))
vlayout.addWidget(QPushButton(str(4)))
vlayout.addWidget(QPushButton(str(5)))
self.setLayout(vlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())

6.4.3 addStrech()函数的使用
在布局时要用到addStretch()函数。设置stretch伸缩量后,按比例分配剩余空间。
| 函数 | 描述 |
|---|---|
| QBoxLayout.addStretch(int stretch = 0) | addStretch()函数在布局管理器中增加一个可伸缩的控件(QSpaceltem),0为最小值,并且将 stretch 作为伸缩量添加到布局末尾;stretch参数表示均分的比例,默认值为0 |

上图为不设置伸缩时的样子
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton
import sys
class WindowDemo(QWidget):
def __init__(self):
super().__init__()
btn1 = QPushButton(self)
btn2 = QPushButton(self)
btn3 = QPushButton(self)
btn1.setText('button 1')
btn2.setText('button 2')
btn3.setText('button 3')
hbox = QHBoxLayout()
# 设置伸缩量为1
# hbox.addStretch(1)
hbox.addWidget(btn1)
# 设置伸缩量为1
# hbox.addStretch(1)
hbox.addWidget(btn2)
# 设置伸缩量为1
# hbox.addStretch(1)
hbox.addWidget(btn3)
# 设置伸缩量为1
hbox.addStretch(1)
self.setLayout(hbox)
self.setWindowTitle("addStretch 例子")
self.resize(600,50)
if __name__ == "__main__":
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = WindowDemo()
win.show()
sys.exit(app.exec_())
设置伸缩后

在第一个控件之前添加伸缩控件,所有控件都会居右显示

同理,在最后一个控件之后添加伸缩控件,所有控件都会居左显示

边栏推荐
- Will the modified data be updated when it is the same as the original data?
- 《UnityShader入门精要》总结(2):初级篇
- How does alternates achieve high-performance publish and subscribe?
- Database system design: partition
- Unity 引导系统.点击目标物体后提示文字变色进入下一步
- 如何为OpenHarmony做贡献
- 36. JS animation
- I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
- 网络原理笔记(五层网络)
- How to export the old and new file names and locations to excel after file renaming
猜你喜欢
![[machine learning] logistic regression code exercise](/img/dc/203f240e2eb213dbd6173d17e47ec6.jpg)
[machine learning] logistic regression code exercise

Flowable 基础篇1

用户身份标识与账号体系实践

Leetcode question brushing (6)

CVPR 2022 | clonedperson: building a large-scale virtual pedestrian data set of real wear and wear from a single photo

【机器学习】逻辑回归代码练习

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

redis可视化工具读取数据乱码问题解决

MySQL error summary
Notes on network principles (five layer network)
随机推荐
Axurerp prototype design starts quickly
数据表示与计算(进制)
Cloud native management practice: business led Devops continuous delivery system
多标签用户画像分析跑得快的关键在哪里?
不用Swagger,那我用啥?
How to realize the isolation level between MySQL transactions and mvcc
dataframe.to_sql() 一次性插入过多报错
常用的DOS命令[逐渐完善]
Study and exploration of Redux API implementation of Redux
mysql怎么换成中文
分布式Session共享的4类技术方案,与优劣势比较
Floweable foundation Chapter 1
40余岁的边缘老技术,是未来乏力还是掘地而起?
(Video + graphics) introduction to machine learning series - Chapter 1 Introduction
Leetcode question brushing (6)
One click automated data analysis! Come and have a look at these treasure tool libraries
Tesseract text recognition -- simple
Using logistic regression and neural network to deal with complex binary classification problems
The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
存算一体与存内计算计算杂谈