python pyqt5 QTableWidget 设置单元格样式

2022-01-10 08:41:13 浏览数 (1)

-- coding: utf-8 --

''' 【简介】 PyQT5中单元格内的文本颜色

'''

import sys from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem) from PyQt5.QtGui import QBrush, QColor, QFont

class Table(QWidget): def init(self): super().init() self.initUI()

代码语言:javascript复制
def initUI(self):
    self.setWindowTitle("QTableWidget 例子")
    self.resize(430, 230);
    conLayout = QHBoxLayout()
    tableWidget = QTableWidget()
    tableWidget.setRowCount(4)
    tableWidget.setColumnCount(3)
    conLayout.addWidget(tableWidget)

    tableWidget.setHorizontalHeaderLabels(['姓名', '性别', '体重(kg)'])

    newItem = QTableWidgetItem("张三")
    newItem.setForeground(QBrush(QColor(255, 0, 0)))
    tableWidget.setItem(0, 0, newItem)

    newItem = QTableWidgetItem("男")
    newItem.setForeground(QBrush(QColor(255, 0, 0)))
    tableWidget.setItem(0, 1, newItem)

    newItem = QTableWidgetItem("160")
    newItem.setForeground(QBrush(QColor(255, 0, 0)))
    tableWidget.setItem(0, 2, newItem)

    self.setLayout(conLayout)

if name == 'main': app = QApplication(sys.argv) example = Table() example.show() sys.exit(app.exec_())

0 人点赞