python GUI庫圖形界面開發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實(shí)例
PyQt5布局控件QHBoxLayout簡介
采用QBOXLayout類可以在水平和垂直方向上排列控件,QHBoxLayout和QVBoxLayout類繼承自QBoxLayout
采用QHBoxLayout類,按照從左到右的順序來添加控件
QHBoxLayout類中常用的方法如下
| 方法 | 描述 |
|---|---|
| addLayout(self,stretch=0) | 在窗口的右邊添加布局,使用stretch(伸縮量)進(jìn)行伸縮,伸縮量默認(rèn)為0 |
| addWidget(self,QWidget.stretch,Qt.Alignmeny alihnment) | 在布局中添加控件 |
| stretch(伸縮量),只適用于QBoxLayout,控件和窗口會隨著伸縮量的變大而增大 | |
| alignment:指定的對齊方式 | |
| addSpacing(self,int) | 設(shè)置各控件的上下間距,通過該方法可以增加額外的控件 |
QHBoxLayout對齊方式參數(shù)
| 參數(shù) | 描述 |
|---|---|
| Qt.AlignLeft | 水平方向居左對齊 |
| Qt.AlignRight水平方向具有對齊 | |
| Qt.AlignCenter | 水平方向居中對齊 |
| Qt.AlignJustify | 水平方向兩端對齊 |
| Qt.AlignTop | 垂直方向靠上對齊 |
| Qt.AlignBottom | 垂直方向靠下對齊 |
| Qt.AlignVCenter | 垂直方向居中對齊 |
QHBoxLayout水平布局管理實(shí)例
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("水平布局管理例子")
# 水平布局按照從左到右的順序進(jìn)行添加按鈕部件。
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 優(yōu)化1 設(shè)置控件間距
#hlayout.setSpacing(20)
self.setLayout(hlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())
運(yùn)行效果圖

優(yōu)化一:設(shè)置各控件之間的間距
hlayout.setSpacing(20)

QHBoxLayout水平布局對齊方式實(shí)例
在某些情況下,需要將布局中的某些控件居中,俱下顯示,那么可以通過對齊方式參數(shù)Qt.Alignment來設(shè)置,示范如下
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)
# 水平布局按照從左到右的順序進(jìn)行添加按鈕部件。
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_())
運(yùn)行效果圖如下

本文主要講解了關(guān)于PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實(shí)例,更多PyQt5布局控件的知識請查看下面的相關(guān)鏈接
相關(guān)文章
Python3 socket即時通訊腳本實(shí)現(xiàn)代碼實(shí)例(threading多線程)
這篇文章主要介紹了Python3 socket即時通訊腳本實(shí)現(xiàn)代碼實(shí)例(threading多線程),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python+PyQt5+MySQL實(shí)現(xiàn)天氣管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python+PyQt5+MySQL實(shí)現(xiàn)天氣管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
python利用K-Means算法實(shí)現(xiàn)對數(shù)據(jù)的聚類案例詳解
這篇文章主要介紹了python利用K-Means算法實(shí)現(xiàn)對數(shù)據(jù)的聚類,本文通過案例講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
詳解python的網(wǎng)絡(luò)編程基礎(chǔ)
這篇文章主要為大家介紹了python網(wǎng)絡(luò)編程的基礎(chǔ),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01
python使用property完成數(shù)據(jù)隱藏封裝與校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了python使用property完成數(shù)據(jù)隱藏封裝與校驗(yàn),實(shí)現(xiàn)安全修改,文中的示例代碼講解詳細(xì),希望對大家有所幫助2024-11-11

