• PySide6 Tutorials (一)表格小部件魔改


    前言

    Pyside6官方教程给了一个使用表格显示颜色的教程,原教程地址如下:源地址, 结合前面button信号的学习,就魔改添加了如下功能:增加一列按钮,可以修改该行的颜色值,通过点击按钮生成指定的颜色背景。

    代码如下
    import sys
    from functools import partial
    from PySide6.QtGui import QColor
    from PySide6.QtWidgets import QApplication, QTableWidget, QPushButton, QTableWidgetItem
    
    
    colors = [("Red", "#FF0000"),
              ("Green", "#00FF00"),
              ("Blue", "#0000FF"),
              ("Black", "#000000"),
              ("White", "#FFFFFF"),
              ("Electric Green", "#41CD52"),
              ("Dark Blue", "#222840"),
              ("Yellow", "#F9E56d")]
    
    
    def get_rgb_from_hex(code):
        code_hex = code.replace("#", "")
        rgb = tuple(int(code_hex[i:i+2], 16) for i in (0, 2, 4))
        return QColor.fromRgb(rgb[0], rgb[1], rgb[2])
    
    
    def change_background(button):
        row = button.property("row")
        col = button.property("col")
    
        if row is not None and col is not None:
            item_color = QTableWidgetItem()
            text = get_rgb_from_hex(table.item(row, 1).text())
            item_color.setBackground(text)
            table.setItem(row, 2, item_color)
    
    
    app = QApplication()
    table = QTableWidget()
    table.setRowCount(len(colors))
    table.setColumnCount(len(colors[0]) + 2)
    table.setHorizontalHeaderLabels(["Name", "Hex Code", "Color", "Change"])
    
    for i, (name, code) in enumerate(colors):
        item_name = QTableWidgetItem(name)
        item_code = QTableWidgetItem(code)
        item_color = QTableWidgetItem()
        item_color.setBackground(get_rgb_from_hex(code))
        table.setItem(i, 0, item_name)
        table.setItem(i, 1, item_code)
        table.setItem(i, 2, item_color)
    
        button = QPushButton("Click me!")
        button.setProperty("row", i)
        button.setProperty("col", 3)
        button.clicked.connect(partial(change_background, button))
    
        table.setCellWidget(i, 3, button)
    
    
    table.show()
    table.resize(600, 800)
    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
    效果如下图

    效果图

    涉及知识点
    • 如何获取表格中button所在的行列信息
    • 如何将button信息传递到信号函数中

    注:本文为作者原创,转载需注明出处!

  • 相关阅读:
    【BIM入门实战】Revit属性对话框中“视图范围”工具的使用方法详解
    (数据结构)数据结构的三要素
    SpringBoot 日志文件
    4.Redis的Key的操作命令
    稳定性实践:限流降级
    好用的VsCode 插件备忘录
    关于多传感器融合方法的总结与思考
    [Spring Boot]09 Spring Boot集成和使用Redis
    java17搭建springboot+JPA+postgreSQL示例项目
    java计算机毕业设计ssm学生过程性评价系统(源码+系统+mysql数据库+Lw文档)
  • 原文地址:https://blog.csdn.net/GodWriter/article/details/134526284