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

运行效果图

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布局控件的知识请查看下面的相关链接

来源:https://blog.csdn.net/jia666666/article/details/81699900

标签:python,PyQt5,布局
0
投稿

猜你喜欢

  • python3 实现爬取TOP500的音乐信息并存储到mongoDB数据库中

    2024-01-21 06:11:12
  • js css 实现遮罩层覆盖其他页面元素附图

    2024-06-08 21:51:06
  • 教你使用Pycharm配置远程Jupyter

    2023-02-09 19:43:06
  • python学习笔记--将python源文件打包成exe文件(pyinstaller)

    2021-04-17 22:03:22
  • MAC下Anaconda+Pyspark安装配置详细步骤

    2021-02-11 18:15:51
  • 在firefox里如何实现firebug的DOM inspect选择功能?

    2010-09-03 18:20:00
  • 详解python 支持向量机(SVM)算法

    2022-03-06 02:11:24
  • 一个css垂直水平居中布局,css效果

    2008-11-03 11:40:00
  • C#连接db2数据库的实现方法

    2024-01-19 07:00:51
  • 如何从零开始利用js手写一个Promise库详解

    2024-04-19 10:46:32
  • Python获取二维矩阵每列最大值的方法

    2022-08-28 00:26:16
  • Python浅析生成器generator的使用

    2023-09-02 16:58:40
  • 深入理解ASP中FSO的神奇功能

    2007-09-18 12:22:00
  • XML入门精解之文件格式定义

    2008-02-25 13:57:00
  • python制作的天气预报小工具(gui界面)

    2022-04-03 17:20:42
  • css去掉checkbox边框的方法

    2011-06-06 10:32:00
  • 基于Python的EasyGUI学习实践

    2021-09-05 10:54:55
  • Mybatis update数据库死锁之获取数据库连接池等待

    2024-01-26 20:40:10
  • 如何正确合理的建立MYSQL数据库索引

    2010-10-25 20:08:00
  • Git基础学习之tag标签操作详解

    2023-01-01 08:27:20
  • asp之家 网络编程 m.aspxhome.com