python GUI库图形界面开发之库图形界面开发之PyQt5布局控件布局控件QHBoxLayout
详细使用方法与实例详细使用方法与实例
主要介绍了python GUI库图形界面开发之PyQt5布局控件QHBoxLayout详细使用方法与实例,需要的朋友可以参
考下
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_())
运行效果图
评论0