PyQt5-箱布局(QHBoxLayout, QVBoxLayout)-9
1 #!/usr/bin/python3 2 # -*- coding: utf-8 -*- 3 4 '''
即横向和纵向布局:QHBoxLayout, QVBoxLayout
''' 5 6 import sys 7 from PyQt5.QtWidgets import (QWidget, QPushButton, 8 QHBoxLayout, QVBoxLayout, QApplication) 9 10 #demo_9箱布局,即QHBoxLayout和QVBoxLayout 11 class Example(QWidget): 12 def __init__(self): 13 super().__init__() 14 15 self.initUI() 16 17 def initUI(self): 18 okButton = QPushButton("OK") 19 cancelButton = QPushButton("Cancel") 20 21 #下面加了两个拉伸因子,(即按比例显示) 22 hbox = QHBoxLayout() 23 hbox.addStretch(1) #与下面1:1 空白拉伸 24 hbox.addWidget(okButton) 25 hbox.addWidget(cancelButton) 26 hbox.addStretch(1) #与上面1:1 空白拉伸 27 28 self.setLayout(hbox) 29 30 self.setGeometry(300, 300, 300, 150) 31 self.setWindowTitle('Buttons') 32 self.show() 33 34 35 if __name__ == '__main__': 36 app = QApplication(sys.argv) 37 ex = Example() 38 sys.exit(app.exec_())
1 #!/usr/bin/python3 2 # -*- coding: utf-8 -*- 3 4 5 6 import sys 7 from PyQt5.QtWidgets import (QWidget, QPushButton, 8 QHBoxLayout, QVBoxLayout, QApplication) 9 10 #demo_10:箱布局 11 class Example(QWidget): 12 def __init__(self): 13 super().__init__() 14 15 self.initUI() 16 17 def initUI(self): 18 okButton = QPushButton("OK") 19 cancelButton = QPushButton("Cancel") 20 21 hbox = QHBoxLayout() 22 hbox.addStretch(1)#默认值为0 , 水平方向,拉伸因子将按钮排列在右侧 23 hbox.addWidget(okButton) 24 hbox.addWidget(cancelButton) 25 26 vbox = QVBoxLayout() 27 vbox.addStretch(1) # 拉伸因子 ,纵向将按钮放在底部 28 vbox.addLayout(hbox) 29 30 self.setLayout(vbox) 31 32 self.setGeometry(300, 300, 300, 150) 33 self.setWindowTitle('Buttons') 34 self.show() 35 36 37 if __name__ == '__main__': 38 app = QApplication(sys.argv) 39 ex = Example() 40 sys.exit(app.exec_())