pyqt 布局篇

grid布局

from PyQt5.Qt import *

class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.resize(500,500)
        self.setup_ui()


    def setup_ui(self):
        grid = QGridLayout()
        self.setLayout(grid)

        names = [
            "CLS","Bck",'',"CLOSE",
            '4','5','6','7',
            '1','2','3','4',
            '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,300)
        self.setWindowTitle("Calculator")
if __name__ == '__main__':
    import sys
    app=QApplication(sys.argv)

    win=Window()
    win.show()
    sys.exit(app.exec_())

分割器


from PyQt5.Qt import *




class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("分割器的学习")
        self.resize(500,500)
        self.setup_ui()


    def setup_ui(self):

        self.layout = QVBoxLayout()
        self.mainSplitter = QSplitter(self)
        self.layout.addWidget(self.mainSplitter)

        self.setLayout(self.layout)
        #水平线割
        self.mainSplitter.setOrientation(Qt.Horizontal)

        rightSplitter = QSplitter(self) #垂直分割
        rightSplitter.setOrientation(Qt.Vertical)

        textEdit = QTextEdit()
        textEdit.setText("window2")
        rightSplitter.addWidget(textEdit)

        t3=QTextEdit()
        t3.setText('window3')
        rightSplitter.addWidget(t3)

        t4=QTextEdit()
        t4.setText("wiindow1")
        self.mainSplitter.addWidget(t4)

        self.mainSplitter.addWidget(rightSplitter)

        #分割比例
        self.mainSplitter.setStretchFactor(0,1)
        self.mainSplitter.setStretchFactor(1,2)
        self.mainSplitter.show()






if __name__ == '__main__':
    import sys
    app=QApplication(sys.argv)

    win=Window()
    win.show()





    sys.exit(app.exec_())






你可能感兴趣的:(pyqt)