相关文章推荐
很拉风的荒野  ·  bat命令删除文件到回收站如何写?_百度知道·  2 年前    · 
睿智的羊肉串  ·  IAT HOOK - 知乎·  2 年前    · 
骑白马的盒饭  ·  IDEA创建单元测试与测试覆盖率统计 - 知乎·  2 年前    · 
Code  ›  python GUI库图形界面开发之PyQt5布局控件QHBoxLayout详细使用方法与实例开发者社区
gui python继承 pyqt5 图形界面
https://cloud.tencent.com/developer/article/1741993
逆袭的鸵鸟
2 年前
作者头像
砸漏
0 篇文章

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

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 恩蓝脚本 > python GUI库图形界面开发之PyQt5布局控件QHBoxLayout详细使用方法与实例

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

作者头像
砸漏
发布 于 2020-11-05 10:27:14
1.3K 0
发布 于 2020-11-05 10:27:14
举报

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)  
 
推荐文章
很拉风的荒野  ·  bat命令删除文件到回收站如何写?_百度知道
2 年前
睿智的羊肉串  ·  IAT HOOK - 知乎
2 年前
骑白马的盒饭  ·  IDEA创建单元测试与测试覆盖率统计 - 知乎
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号