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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM