天天看點

qt qlabel 布局重疊_Pyqt5布局管理執行個體

Python GUI布局方式:

  • 絕對定位布局(move())
  • 水準盒(QHBoxLayout())
  • 垂直盒(QVBoxLayout())

一、絕對定位布局

每個控件通過move()設定具體位置

執行個體:

import sysfrom PyQt5.QtWidgets import QApplication, QWidgetclass Demo(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        label1 = QLabel('demo1',self)        label1.move(10,20)        label2 = QLabel('demo2',self)        label2.move(10,40)        label3 = QLabel('demo3', self)        label3.move(10, 60)        self.setGeometry(300, 300, 250, 150)        self.setWindowTitle('絕對定位')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())
           

效果:

qt qlabel 布局重疊_Pyqt5布局管理執行個體

二、水準盒布局

1、使控件在GUI界面始終處于(預設)水準居中位置,可設定伸縮量對齊

執行個體:

from PyQt5.QtWidgets import *import sysclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        self.layout = QHBoxLayout()        self.label1 = QLabel("demo1")        self.label2 = QLabel("demo2")        self.label3 = QLabel("demo3")        self.label3 = QLabel("demo4")        self.label3 = QLabel("demo5")                #水準控件的預設位置        self.layout.addWidget(self.label1)        self.layout.addWidget(self.label2)        self.layout.addWidget(self.label3)        self.layout.addWidget(self.label4)        self.layout.addWidget(self.label5)                #設定水準盒布局控件的拉伸        self.layout.addWidget(self.label1, 2, Qt.AlignLeft | Qt.AlignTop)        self.layout.addWidget(self.label2, 2, Qt.AlignLeft | Qt.AlignTop)        self.layout.addWidget(self.label3, 1, Qt.AlignLeft | Qt.AlignTop)        self.layout.addWidget(self.label4, 0, Qt.AlignRight | Qt.AlignBottom)        self.layout.addWidget(self.label5, 1, Qt.AlignRight | Qt.AlignBottom)        # 設定水準盒布局的控件間距大小        self.layout.setSpacing(20)        self.setLayout(self.layout)        self.setGeometry(300, 300, 250, 150)        self.setWindowTitle('水準盒')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())
           

效果:

qt qlabel 布局重疊_Pyqt5布局管理執行個體

三、垂直盒布局

控件垂直布局,并設定按鈕一直在右下角

執行個體:

from PyQt5.QtWidgets import *from  PyQt5.QtCore import *import sysclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        self.resize(400, 300)        self.ok = QPushButton("确定")        self.cancel = QPushButton("取消")        self.h = QHBoxLayout()        self.h.addStretch(1)        self.h.addWidget(self.ok)        self.h.addWidget(self.cancel)        self.v = QVBoxLayout()        self.bt1 = QPushButton("demo1")        self.bt2 = QPushButton("demo2")        self.bt3 = QPushButton("demo3")        self.v.addStretch(0)  # 放在上面        self.v.addWidget(self.bt1)        self.v.addWidget(self.bt2)        self.v.addWidget(self.bt3)        self.v.addStretch(1)  # 始終保持在放在右下角        self.v.addLayout(self.h)        self.setLayout(self.v)        self.setGeometry(300, 300, 250, 150)        self.setWindowTitle('按鍵視窗')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())
           

效果:

qt qlabel 布局重疊_Pyqt5布局管理執行個體

四、網格布局

執行個體:

from PyQt5.QtWidgets import *from  PyQt5.QtCore import *import sysclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):                        #建立一個網格的按鈕        grid = QGridLayout()        self.setLayout(grid)        #QGridLayout的執行個體被建立并設定應用程式視窗的布局        names = ['Cls', 'Bck', '', 'Close',                 '7', '8', '9', '/',                 '4', '5', '6', '*',                 '1', '2', '3', '-',                 '0', '.', '=', '+']        #按鈕的标簽        positions = [(i, j) for i in range(5) for j in range(4)]        # 建立一個網格中的位置的清單        for position, name in zip(positions, names):            if name == '':                continue            button = QPushButton(name)            grid.addWidget(button, *position)        self.move(300, 150)        self.setWindowTitle('網格布局')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())
           

效果:

qt qlabel 布局重疊_Pyqt5布局管理執行個體

三、表單布局

執行個體:

from PyQt5.QtWidgets import *from  PyQt5.QtCore import *import sysclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        form = QFormLayout()        label1 = QLabel('标題')        label2 = QLabel('内容')        label3 = QLabel('評論')        l1 = QLineEdit()        l2 = QLineEdit()        l3 = QTextEdit()        form.addRow(label1, l1)        form.addRow(label2, l2)        form.addRow(label3, l3)        self.setLayout(form)        self.move(300, 150)        self.setWindowTitle('form')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())
           

效果:

qt qlabel 布局重疊_Pyqt5布局管理執行個體