python GUI库图形界面开发之PyQt5布局控件QHBoxLayout详细使用方法与实例
作者:jia666666 时间:2021-05-05 15:44:10
PyQt5布局控件QHBoxLayout简介
采用QBOXLayout类可以在水平和垂直方向上排列控件,QHBoxLayout和QVBoxLayout类继承自QBoxLayout
采用QHBoxLayout类,按照从左到右的顺序来添加控件
QHBoxLayout类中常用的方法如下
方法 | 描述 |
---|---|
addLayout(self,stretch=0) | 在窗口的右边添加布局,使用stretch(伸缩量)进行伸缩,伸缩量默认为0 |
addWidget(self,QWidget.stretch,Qt.Alignmeny alihnment) | 在布局中添加控件 |
stretch(伸缩量),只适用于QBoxLayout,控件和窗口会随着伸缩量的变大而增大 | |
alignment:指定的对齐方式 | |
addSpacing(self,int) | 设置各控件的上下间距,通过该方法可以增加额外的控件 |
QHBoxLayout对齐方式参数
参数 | 描述 |
---|---|
Qt.AlignLeft | 水平方向居左对齐 |
Qt.AlignRight水平方向具有对齐 | |
Qt.AlignCenter | 水平方向居中对齐 |
Qt.AlignJustify | 水平方向两端对齐 |
Qt.AlignTop | 垂直方向靠上对齐 |
Qt.AlignBottom | 垂直方向靠下对齐 |
Qt.AlignVCenter | 垂直方向居中对齐 |
QHBoxLayout水平布局管理实例
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)))
# todo 优化1 设置控件间距
#hlayout.setSpacing(20)
self.setLayout(hlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())
运行效果图
优化一:设置各控件之间的间距
hlayout.setSpacing(20)
QHBoxLayout水平布局对齐方式实例
在某些情况下,需要将布局中的某些控件居中,俱下显示,那么可以通过对齐方式参数Qt.Alignment来设置,示范如下
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_())
运行效果图如下
本文主要讲解了关于PyQt5布局控件QHBoxLayout详细使用方法与实例,更多PyQt5布局控件的知识请查看下面的相关链接
来源:https://blog.csdn.net/jia666666/article/details/81699900
标签:python,PyQt5,布局
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pytest内置fixture使用临时目录流程详解
2021-12-27 06:49:23
Python numpy大矩阵运算内存不足如何解决
2022-08-06 22:38:21
![](https://img.aspxhome.com/file/2023/9/92509_0s.png)
详解Bagging算法的原理及Python实现
2021-06-10 00:20:41
![](https://img.aspxhome.com/file/2023/9/77909_0s.jpg)
用户研究中的人物角色
2010-02-08 12:15:00
一篇文章带你了解python标准库--random模块
2023-09-18 10:45:34
![](https://img.aspxhome.com/file/2023/6/83836_0s.png)
PHP如何利用P3P实现跨域
2023-11-23 20:16:58
Matplotlib中%matplotlib inline如何使用
2021-11-22 15:17:41
![](https://img.aspxhome.com/file/2023/4/89314_0s.png)
Python OpenCV之常用滤波器使用详解
2023-02-07 22:18:25
![](https://img.aspxhome.com/file/2023/9/81589_0s.jpg)
SQLServer WITH 的用法
2009-07-09 18:54:00
Python 经典面试题 21 道【不可错过】
2023-10-29 04:56:20
PHP实现的XXTEA加密解密算法示例
2023-07-13 22:29:49
Python实现一键整理百度云盘中重复无用文件
2023-09-23 04:33:38
![](https://img.aspxhome.com/file/2023/7/93957_0s.png)
Python Pandas批量读取csv文件到dataframe的方法
2022-12-15 17:05:03
![](https://img.aspxhome.com/file/2023/7/77787_0s.jpg)
符合w3c标准flash插入代码,常用flash参数设置
2009-01-20 18:47:00
python使用RNN实现文本分类
2023-10-17 14:07:18
![](https://img.aspxhome.com/file/2023/1/96461_0s.jpg)
在sql Server自定义一个用户定义星期函数
2012-02-12 15:47:28
基于Django与ajax之间的json传输方法
2023-01-12 19:52:58
Python实现外星人去哪了小游戏详细代码
2023-08-07 13:42:44
另外一种斜体的导航条
2008-11-05 12:24:00
Python 图像处理: 生成二维高斯分布蒙版的实例
2023-01-31 16:29:06
![](https://img.aspxhome.com/file/2023/8/81538_0s.jpg)