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


Posted in Python onMarch 06, 2020

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_())

运行效果图

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

优化一:设置各控件之间的间距

hlayout.setSpacing(20)

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

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_())

运行效果图如下

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

本文主要讲解了关于PyQt5布局控件QHBoxLayout详细使用方法与实例,更多PyQt5布局控件的知识请查看下面的相关链接

Python 相关文章推荐
Windows下PyMongo下载及安装教程
Apr 27 Python
python中threading超线程用法实例分析
May 16 Python
Python简单删除目录下文件以及文件夹的方法
May 27 Python
Python的Flask站点中集成xhEditor文本编辑器的教程
Jun 13 Python
python如何派生内置不可变类型并修改实例化行为
Mar 21 Python
python爬取网页转换为PDF文件
Jun 07 Python
Python时间序列缺失值的处理方法(日期缺失填充)
Aug 11 Python
Python3.6 中的pyinstaller安装和使用教程
Mar 16 Python
Python读取excel文件中带公式的值的实现
Apr 17 Python
13个Pandas实用技巧,助你提高开发效率
Aug 19 Python
Python Pivot table透视表使用方法解析
Sep 11 Python
Pandas数据结构之Series的使用
Mar 31 Python
Python yield的用法实例分析
Mar 06 #Python
用Python生成HTML表格的方法示例
Mar 06 #Python
使用Python第三方库pygame写个贪吃蛇小游戏
Mar 06 #Python
Python修改列表值问题解决方案
Mar 06 #Python
浅谈matplotlib.pyplot与axes的关系
Mar 06 #Python
python-xpath获取html文档的部分内容
Mar 06 #Python
关于python中的xpath解析定位
Mar 06 #Python
You might like
Yii2创建控制器(createController)方法详解
2016/07/23 PHP
php flush无效,IIS7下php实时输出的方法
2016/08/25 PHP
使用ThinkPHP生成缩略图及显示
2017/04/27 PHP
微信接口生成带参数的二维码
2017/07/31 PHP
javascript 读取xml,写入xml 实现代码
2009/07/10 Javascript
jquery判断checkbox(复选框)是否被选中的代码
2010/10/20 Javascript
基于jQuery的图片左右无缝滚动插件
2012/05/23 Javascript
子窗体与父窗体传值示例js代码
2013/08/01 Javascript
select隐藏选中值对应的id,显示其它id的简单实现方法
2016/08/25 Javascript
BootStrap轮播HTML代码(推荐)
2016/12/10 Javascript
jQuery向webApi提交post json数据
2017/01/16 Javascript
微信小程序五星评分效果实现代码
2017/04/06 Javascript
JavaScript中的遍历详解(多种遍历)
2017/04/07 Javascript
详解vue+webpack+express中间件接口使用
2018/07/17 Javascript
Vue监听事件实现计数点击依次增加的方法
2018/09/26 Javascript
js序列化和反序列化的使用讲解
2019/01/19 Javascript
JS定义函数的几种常用方法小结
2019/05/23 Javascript
python实现文本去重且不打乱原本顺序
2016/01/26 Python
Python实现多线程抓取网页功能实例详解
2017/06/08 Python
python连接mongodb密码认证实例
2018/10/16 Python
Django数据库类库MySQLdb使用详解
2019/04/28 Python
基于python判断字符串括号是否闭合{}[]()
2020/09/21 Python
python查询MySQL将数据写入Excel
2020/10/29 Python
Python在线和离线安装第三方库的方法
2020/10/31 Python
Weekendesk意大利:探索多种引人入胜的周末主题
2016/10/14 全球购物
一道输出判断型Java面试题
2014/10/01 面试题
销售自我评价
2013/10/22 职场文书
环境科学专业个人求职信
2013/12/15 职场文书
高一新生军训感言
2014/03/02 职场文书
社保缴纳证明申请书
2014/11/03 职场文书
幼儿园园长新年寄语2015
2014/12/08 职场文书
交通安全温馨提示语
2015/07/14 职场文书
读完《骆驼祥子》的观后感!
2019/07/05 职场文书
感恩信:写给爸爸妈妈的一封感谢信
2019/09/12 职场文书
修改MySQL的默认密码的四种小方法
2021/05/26 MySQL
Python使用Kubernetes API访问集群
2021/05/30 Python