• PyQt5 布局Layout


    QHBoxLayout 水平布局

    import sys
    from PyQt5.QtWidgets import QDesktopWidget, QApplication, QMainWindow, QWidget, QHBoxLayout, QPushButton
    
    class MainWindow(QMainWindow):
    
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.initUI()
    
    
        def initUI(self):
            self.resize(300, 200)
            self.setWindowTitle("QHBoxLayout水平布局")
    
            hlayout = QHBoxLayout()
    
            hlayout.addWidget(QPushButton('1'))
            hlayout.addWidget(QPushButton('2'))
            hlayout.addWidget(QPushButton('3'))
            hlayout.addWidget(QPushButton('4'))
    
            mainwidget = QWidget()
            mainwidget.setLayout(hlayout)
            self.setCentralWidget(mainwidget)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MainWindow()
        win.show()
        sys.exit(app.exec_())
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    在这里插入图片描述

    QVBoxLayout垂直布局

    import sys
    from PyQt5.QtWidgets import QDesktopWidget, QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton
    
    class MainWindow(QMainWindow):
    
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.initUI()
    
        def initUI(self):
            self.setGeometry(100, 100, 300, 300)
            self.setWindowTitle("QVBoxLayout垂直布局")
    
            vlayout = QVBoxLayout()
            vlayout.addWidget(QPushButton('1'))
            vlayout.addWidget(QPushButton('2'))
            vlayout.addWidget(QPushButton('3'))
            vlayout.addWidget(QPushButton('4'))
            vlayout.addWidget(QPushButton('5'))
    
            mwidget = QWidget()
            mwidget.setLayout(vlayout)
            self.setCentralWidget(mwidget)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MainWindow()
        win.show()
        sys.exit(app.exec_())
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

    QGridLayout栅格布局

    import sys
    from PyQt5.QtWidgets import QDesktopWidget, QMainWindow, QApplication, QGridLayout, QPushButton, QWidget
    
    
    class MainWindow(QMainWindow):
    
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.initUI()
    
        def initUI(self):
            self.resize(500, 200)
            self.setWindowTitle("GridLayout栅格布局")
            gridlayout = QGridLayout()
            
            gridlayout.addWidget(QPushButton("00"), 0, 0)
            gridlayout.addWidget(QPushButton("01"), 0, 1)
            gridlayout.addWidget(QPushButton("02"), 0, 2)
    
            gridlayout.addWidget(QPushButton("10"), 1, 0)
            gridlayout.addWidget(QPushButton("11"), 1, 1)
            gridlayout.addWidget(QPushButton("12"), 1, 2)
    
            gridlayout.addWidget(QPushButton("20"), 2, 0)
            gridlayout.addWidget(QPushButton("21"), 2, 1)
            gridlayout.addWidget(QPushButton("22"), 2, 2)
        
            mwidget = QWidget()
            mwidget.setLayout(gridlayout)
            self.setCentralWidget(mwidget)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MainWindow()
        win.show()
        sys.exit(app.exec_())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    在这里插入图片描述

    QFormLayout表单布局

    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import Qt
    
    class MainWindow(QMainWindow):
    
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
           
            self.initUI()
    
        def initUI(self):
            self.resize(400, 200)
            self.setWindowTitle('FormLayout表单布局')
        
            formlayout = QFormLayout()
            formlayout.setLabelAlignment(Qt.AlignRight)
            
            normalLineEdit = QLineEdit()
            normalLineEdit.setAlignment(Qt.AlignRight)
            normalLineEdit.setPlaceholderText("请输入密码Normal")
            normalLineEdit.setEchoMode(QLineEdit.Normal)
    
            noechoLineEdit = QLineEdit()
            noechoLineEdit.setAlignment(Qt.AlignLeft)
            noechoLineEdit.setPlaceholderText("请输入密码NoEcho")
            noechoLineEdit.setEchoMode(QLineEdit.NoEcho)
    
            passwordLineEdit = QLineEdit()
            passwordLineEdit.setAlignment(Qt.AlignRight)
            passwordLineEdit.setPlaceholderText("请输入密码Password")
            passwordLineEdit.setEchoMode(QLineEdit.Password)
            
            passwordEchoOnEdit = QLineEdit()
            passwordEchoOnEdit.setAlignment(Qt.AlignLeft)
            passwordEchoOnEdit.setPlaceholderText("请输入密码PasswordEcho")
            passwordEchoOnEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
            
            formlayout.addRow("Normal:", normalLineEdit)
            formlayout.addRow("EchoMode:", noechoLineEdit)
            formlayout.addRow("Password:", passwordLineEdit)
            formlayout.addRow("Password Echo:", passwordEchoOnEdit)
    
    
            mwidget = QWidget()
            mwidget.setLayout(formlayout)        
            self.setCentralWidget(mwidget)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MainWindow()
        win.show()
        sys.exit(app.exec_())
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    在这里插入图片描述

    QStackedLayout堆栈布局

    在这里插入图片描述

    import sys
    from PyQt5.QtWidgets import (QApplication, QMainWindow, QStackedLayout, QWidget, QPushButton, QVBoxLayout, QHBoxLayout,
        QToolBar, QToolButton, QColorDialog, QFontDialog, QFileDialog, QMessageBox, QStyle)
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import Qt
    
    class MyStackedLayout(QMainWindow):
        def __init__(self, parent=None):
            super(MyStackedLayout, self).__init__(parent)
            self.initUI()
    
        def initUI(self):
            self.resize(800, 480)
            self.move(300, 300)
            self.setWindowTitle("StackedLayout堆栈布局")
    
            toolBar = QToolBar(self)    # 创建ToolBar
            self.addToolBar(Qt.LeftToolBarArea, toolBar) # 添加ToolBar到主界面
            
            # 创建一个ToolButton
            btnColor = QToolButton(self)
            btnColor.setText("颜色对话框")
            btnColor.setIcon(QApplication.style().standardIcon(QStyle.SP_ComputerIcon))
            btnColor.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
            btnColor.clicked.connect(lambda: self.onButtonClicked(0))
            toolBar.addWidget(btnColor) # ToolBar添加ToolButton按钮
    
            btnFont = QToolButton(self)
            btnFont.setText("字体对话框")
            btnFont.setIcon(QApplication.style().standardIcon(QStyle.SP_DesktopIcon))
            btnFont.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
            btnFont.clicked.connect(lambda: self.onButtonClicked(1))
            toolBar.addWidget(btnFont)
    
            btnFile = QToolButton(self)
            btnFile.setText("文件对话框")
            btnFile.setIcon(QApplication.style().standardIcon(QStyle.SP_DialogOpenButton))
            btnFile.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
            btnFile.clicked.connect(lambda: self.onButtonClicked(2))
            toolBar.addWidget(btnFile)
            
            self.mainlayout = QStackedLayout()
            self.mainlayout.addWidget(QColorDialog(self))# StackedLayout添加一个Widget,  index为0
            self.mainlayout.addWidget(QFontDialog(self)) # StackedLayout添加一个Widget,  index为1
            self.mainlayout.addWidget(QFileDialog(self)) # StackedLayout添加一个Widget,  index为2
    
            self.mainwidget = QWidget()
            self.mainwidget.setLayout(self.mainlayout)
            self.setCentralWidget(self.mainwidget)
        
        def onButtonClicked(self, index):
            if index < self.mainlayout.count():
                self.mainlayout.setCurrentIndex(index)
            
    if __name__ ==  "__main__":
        app = QApplication(sys.argv)
        win = MyStackedLayout()
        win.show()
        sys.exit(app.exec_())
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    在这里插入图片描述

    QStackedWidget堆栈控件

    QStackedWidget和QStackedLayout用法类似,
    QStackedWidget直接创建的是Widget对象
    QStackedLayout创建的是Layout布局,需要添加到QWidget对象之后才能设置为中心控件。

    import sys
    from PyQt5.QtCore import Qt
    from PyQt5.QtGui import QIcon
    from PyQt5.QtWidgets import QApplication, QMainWindow, QStackedWidget, QStyle, QToolBar, QToolButton, QColorDialog, QFileDialog, QFontDialog
    
    class MyStackedWidget(QMainWindow):
        def __init__(self, parent=None):
            super(MyStackedWidget, self).__init__(parent)
            self.initUI()
    
        def initUI(self):
            self.resize(800, 480)
            self.setWindowTitle("StackedWidget控件")
    
            toolBar = QToolBar(self)
            self.addToolBar(Qt.LeftToolBarArea,toolBar)
    
            btnColor = QToolButton(self)
            btnColor.setText("颜色对话框")
            btnColor.setIcon(QApplication.style().standardIcon(QStyle.SP_ArrowRight))
            btnColor.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
            btnColor.clicked.connect(lambda: self.onToolButtonClicked(0))
            toolBar.addWidget(btnColor)
    
            btnFont = QToolButton(self)
            btnFont.setText("字体对话框")
            btnFont.setIcon(QApplication.style().standardIcon(QStyle.SP_ArrowBack))
            btnFont.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
            btnFont.clicked.connect(lambda: self.onToolButtonClicked(1))
            toolBar.addWidget(btnFont)
    
            btnFile = QToolButton(self)
            btnFile.setText("文件对话框")
            btnFile.setIcon(QApplication.style().standardIcon(QStyle.SP_ArrowDown))
            btnFile.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
            btnFile.clicked.connect(lambda: self.onToolButtonClicked(2))
            toolBar.addWidget(btnFile)
                    
            self.mainwidget = QStackedWidget(self)
            self.mainwidget.addWidget(QColorDialog(self))
            self.mainwidget.addWidget(QFontDialog(self))
            self.mainwidget.addWidget(QFileDialog(self))
    
            self.setCentralWidget(self.mainwidget)
    
        def onToolButtonClicked(self, index):
            if index < self.mainwidget.count():
                self.mainwidget.setCurrentIndex(index)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MyStackedWidget()
        win.show()
        sys.exit(app.exec_())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    在这里插入图片描述

  • 相关阅读:
    LeetCode235. Lowest Common Ancestor of a Binary Search Tree
    django带了一个权限系统
    深入浅出三户模型
    定制化、高可用前台样式处理方案——tailwindcss
    python常用的可视化工具(库)
    ETCD数据库源码分析——etcd gRPC 服务 API
    爱上C语言:操作符详解(下)
    .Net8 Blazor 尝鲜
    RPC接口测试-两种方法(Jmeter和代码)
    通过leangoo建立你的清单管理系统
  • 原文地址:https://blog.csdn.net/u013420428/article/details/127883835