PyQT5基礎布局管理


絕對定位布局

使用move(x, y)可以對窗口進行布局,以窗口左上角為原點,向右為 x 軸正方向,向下為 y 軸正方向,移動(x,y);

import sys

from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QWidget, QLabel, QApplication


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        font=QFont("微軟雅黑",12)
        lbl1 = QLabel('java', self)
        lbl1.move(15, 10)

        lbl2 = QLabel('python', self)
        lbl2.move(35, 40)

        lbl3 = QLabel('c++', self)
        lbl3.move(55, 70)

        lbl1.setFont(font)
        lbl2.setFont(font)
        lbl3.setFont(font)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Program Language')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

橫向布局和縱向布局

使用QHBoxLayoutQVBoxLayout,來分別創建橫向布局和縱向布局

addStretch():Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.

addStretch()添加彈簧伸縮量,如在按鈕左側添加彈簧,則拉伸時按鈕會貼近右邊界。兩側添加彈簧量相同,則拉伸時居中。彈簧其實是平分了空白區域。

setGeometry (0, 0, 30, 35) 四個參數:
從屏幕上(0,0)位置開始(即為最左上角的點),顯示一個30*35的界面(寬30,高35)。

import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,
                             QHBoxLayout, QVBoxLayout, QApplication)


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        rightButton = QPushButton("Right")

        qhLayout = QHBoxLayout()
        qvLayout = QVBoxLayout()

        qhLayout.addStretch(1)

        qhLayout.addWidget(okButton)
        qhLayout.addWidget(cancelButton)
        qvLayout.addStretch(1)
        qvLayout.addLayout(qhLayout)
        qvLayout.addWidget(rightButton)

        self.setLayout(qvLayout)

        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('Buttons')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

表格布局

import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout,
                             QPushButton, QApplication)


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        grid = QGridLayout()
        self.setLayout(grid)

        names = ['X', 'Y', '', 'Z',
                 '(1,0)', '(1,1)', '(1,2)', '(1,3)',
                 '(2,0)', '(2,1)', '(2,2)', '(2,3)',
                 '(3,0)', '(3,1)', '(3,2)', '(3,3)',
                 '(4,0)', '(4,1)', '(4,2)', '(4,3)']

        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('Demo')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


免責聲明!

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



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