python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實(shí)例
采用QBOXLayout類(lèi)可以在水平和垂直方向上排列控件,QHBoxLayout和QVBoxLayout類(lèi)繼承自QBoxLayout
采用QHBoxLayout類(lèi),按照從左到右的順序來(lái)添加控件
QHBoxLayout類(lèi)中常用的方法如下 方法 描述 addLayout(self,stretch=0) 在窗口的右邊添加布局,使用stretch(伸縮量)進(jìn)行伸縮,伸縮量默認(rèn)為0 addWidget(self,QWidget.stretch,Qt.Alignmeny alihnment) 在布局中添加控件 stretch(伸縮量),只適用于QBoxLayout,控件和窗口會(huì)隨著伸縮量的變大而增大 alignment:指定的對(duì)齊方式 addSpacing(self,int) 設(shè)置各控件的上下間距,通過(guò)該方法可以增加額外的控件 QHBoxLayout對(duì)齊方式參數(shù) 參數(shù) 描述 Qt.AlignLeft 水平方向居左對(duì)齊 Qt.AlignRight水平方向具有對(duì)齊 Qt.AlignCenter 水平方向居中對(duì)齊 Qt.AlignJustify 水平方向兩端對(duì)齊 Qt.AlignTop 垂直方向靠上對(duì)齊 Qt.AlignBottom 垂直方向靠下對(duì)齊 Qt.AlignVCenter 垂直方向居中對(duì)齊 QHBoxLayout水平布局管理實(shí)例import sysfrom PyQt5.QtWidgets import QApplication ,QWidget ,QHBoxLayout , QPushButtonclass 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)行效果圖

hlayout.setSpacing(20)

在某些情況下,需要將布局中的某些控件居中,俱下顯示,那么可以通過(guò)對(duì)齊方式參數(shù)Qt.Alignment來(lái)設(shè)置,示范如下
import sysfrom PyQt5.QtWidgets import QApplication ,QWidget ,QHBoxLayout , QPushButtonfrom 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布局控件的知識(shí)請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章:
1. JS中6個(gè)對(duì)象數(shù)組去重的方法2. Python基于requests庫(kù)爬取網(wǎng)站信息3. 資深程序員:給Python軟件開(kāi)發(fā)測(cè)試的25個(gè)忠告!4. Python使用Selenium自動(dòng)進(jìn)行百度搜索的實(shí)現(xiàn)5. 一文帶你徹底理解Java序列化和反序列化6. python中文本字符處理的簡(jiǎn)單方法記錄7. PHP利用curl發(fā)送HTTP請(qǐng)求的實(shí)例代碼8. PHP laravel實(shí)現(xiàn)導(dǎo)出PDF功能9. vscode運(yùn)行php報(bào)錯(cuò)php?not?found解決辦法10. Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求

網(wǎng)公網(wǎng)安備