• pyqt5 学习笔记五(QGridLayout)网格布局


    pyqt5 学习笔记五 (布局:QGridLayout)

    写在前面:
    ①本教程使用pycharm编译器进行pyqt5的学习,安装教程请大家自行百度
    ②本系列博客根据B站王铭东博主教程学习 学习代码 笔记5
    链接:https://pan.baidu.com/s/1i3y4mI_9N84iAC6tav-j8Q
    提取码:gupx
    资料:
    【1】B站教程
    【2】B站教程配套课件
    【3】PyQt官网的所有模块 (有部分内容没有补充进去)
    【4】C++具体实现的API文档

    布局器

    共有四种:
    QBoxLayout:垂直水平布局器
    QGridLayout网格布局器
    QFormLayout:表单布局器
    QStackedLayout:抽屉布局器

    (一)字典

    参考链接:
    【1】python字典:https://blog.csdn.net/weixin_40581859/article/details/126235852
    【2】enumerate() 函数:https://www.runoob.com/python/python-func-enumerate.html

    1、字典的遍历 items()

    data = {
       0: ["7", "8", "9", "+", "("],
       1: ["4", "5", "6", "-", ")"],
       2: ["1", "2", "3", "*", "<-"],
       3: ["0", ".", "=", "/", "C"]
    }
    for temp in data.items():
       print(temp)
    for temp1, temp2 in data.items():
       print("temp1:", temp1)
       print("temp2:", temp2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    temp输出:

    (0, ['7', '8', '9', '+', '('])
    (1, ['4', '5', '6', '-', ')'])
    (2, ['1', '2', '3', '*', '<-'])
    (3, ['0', '.', '=', '/', 'C'])
    
    • 1
    • 2
    • 3
    • 4

    temp1与temp2输出:

    temp1: 0
    temp2: ['7', '8', '9', '+', '(']
    temp1: 1
    temp2: ['4', '5', '6', '-', ')']
    temp1: 2
    temp2: ['1', '2', '3', '*', '<-']
    temp1: 3
    temp2: ['0', '.', '=', '/', 'C']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2、enumerate ()

    enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标

    data = {
       0: ["7", "8", "9", "+", "("],
       1: ["4", "5", "6", "-", ")"],
       2: ["1", "2", "3", "*", "<-"],
       3: ["0", ".", "=", "/", "C"]
    }
    for temp1, temp2 in data.items():
       for col_num, num in enumerate(temp2):
           print("temp1:", col_num)
           print("temp2:", num)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    输出的部分值:分别得到每行的索引(从0开始)以及对应的数据
    在这里插入图片描述

    (二)QGridLayout

    1、QGridLayout用法

    layout = QGridLayout () : 创建网格布局器
    layout.addWidget(btn, line_num,col_num) : 将控件放到相应位置

    import sys
    from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QPushButton
    class MyWindow(QWidget):
       def __init__(self):
           super().__init__()
           self.initui()
       def initui(self):
           layout = QGridLayout()
           data = {
               0: ["7", "8", "9", "+", "("],
               1: ["4", "5", "6", "-", ")"],
               2: ["1", "2", "3", "*", "<-"],
               3: ["0", ".", "=", "/", "C"]
           }
           for line_num, temp_data in data.items():
               for col_num, tp in enumerate(temp_data):
                   print(tp)
                   btn = QPushButton(tp)
                   # 将按钮添加到第几行第几个
                   layout.addWidget(btn, line_num,col_num)
           self.setLayout(layout)
    # 在窗口中添加格子按钮,使得与输入的data位置对应
    if __name__ == '__main__':
       app = QApplication(sys.argv)
       w = MyWindow()
       w.show()
       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

    在这里插入图片描述

    2、QGridLayout 跨行、跨列

    layout.addWidget(btn,3,3,1,2):跨行的控件,起始行,起始列,跨越的行数,跨越列数
    layout.addWidget(QWidget widget,int fromRow,int fromColulmn,int rowSpan,int columnSpan,Qt.Alignment alignment=0)l

            layout = QGridLayout()       
            data = {
                0: ["7", "8", "9", "+", "("],
                1: ["4", "5", "6", "-", ")"],
                2: ["1", "2", "3", "*", "<-"],
                3: ["0", ".", "=", "C"]
            }
            for line_num, temp_data in data.items():
                for col_num, tp in enumerate(temp_data):
                    print(tp)
                    btn = QPushButton(tp)
                    # 将按钮添加到第几行第几个
                    if line_num == 3 and col_num >= 3:
                        btn = QPushButton(tp)
                        layout.addWidget(btn, line_num, col_num, 1, 2)
                    else:
                        layout.addWidget(btn, line_num,col_num)
            self.setLayout(layout)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    3、QGridLayout 布局加入到垂直布局中

    edit控件:

    edit = QLineEdit()
    edit.setPlaceholderText("计算结果")
    container.addWidget(edit)
    
    • 1
    • 2
    • 3

    加控件:layout.addWidget(btn)
    加布局:container.addLayout(grid)

    import sys
    from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QPushButton, QVBoxLayout, QLineEdit
    class MyWindow(QWidget):
        def __init__(self):
            super().__init__()
            self.initui()
        def initui(self):
            container = QVBoxLayout()
            # 1、edit
            edit = QLineEdit()
            edit.setPlaceholderText("计算结果")
            container.addWidget(edit)
            # 2、grid
            grid = QGridLayout()
            data = {
                0: ["7", "8", "9", "+", "("],
                1: ["4", "5", "6", "-", ")"],
                2: ["1", "2", "3", "*", "<-"],
                3: ["0", ".", "=", "/", "C"]
            }
            for line_num, temp_data in data.items():
                for col_num, tp in enumerate(temp_data):
                    print(tp)
                    btn = QPushButton(tp)
                    # 将按钮添加到第几行第几个
                    grid.addWidget(btn, line_num,col_num)
            # 将网格布局加入到垂直布局中
            container.addLayout(grid)
            self.setLayout(container)
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = MyWindow()
        w.show()
        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

    在这里插入图片描述)

  • 相关阅读:
    使用hyper-V 编译和调试Android13(android-13.0.0_r3)源码
    [运维|数据库] MySQL 中的COLLATE在 PostgreSQL如何表示
    算法设计与分析 SCAU17104 视频流有效调度
    HJ20 密码验证合格程序
    探索Java中最常用的框架:Spring、Spring MVC、Spring Boot、MyBatis和Netty
    中国医学影像设备行业市场供需预测及投资价值研究报告
    python基于轻量级卷积神经网络模型开发构建眼疾识别系统
    计算机视觉——飞桨深度学习实战-深度学习网络模型
    react native 使用react-native-web运行在web端(自定义webpack.config.js配置篇)
    SpringBoot_项目打包部署
  • 原文地址:https://blog.csdn.net/weixin_40581859/article/details/126265142