PyQt5基础学习-QFont(构造字体和颜色) 1.QTabelWidgetItem().setFont(QFont(设置表格的属性)) 2.QTabelWidgetItem().setForegroud(设置前景) 3.QTabelWidgetItem().setBackgroud(设置背景) 4.QTableWidget().setItem(设置表格的内容)
设置字体的颜色和大小, 设置前景和背景
""" 设置单元格的字体和颜色 """ import sys from PyQt5.QtWidgets import * from PyQt5 import QtCore from PyQt5.QtGui import QColor, QBrush from PyQt5 import QtWidgets from PyQt5.QtGui import QFont class CellFontAndColor(QWidget): def __init__(self): super(CellFontAndColor, self).__init__() self.initUI() def initUI(self): self.setWindowTitle("设置单元格字体和颜色") self.resize(430, 230) layout = QHBoxLayout() tableWidget = QTableWidget() tableWidget.setRowCount(4) tableWidget.setColumnCount(3) layout.addWidget(tableWidget) tableWidget.setHorizontalHeaderLabels(['姓名', '性别', '体重(kg)']) newItem = QTableWidgetItem('雷神') #设置字体的大小和颜色 newItem.setFont(QFont('times', 14, QFont.Black)) #设置字体的前景 newItem.setForeground(QBrush(QColor(255, 0, 0))) #填充表格里面的内容 tableWidget.setItem(0, 0, newItem) newItem = QTableWidgetItem('死亡女神') newItem.setForeground(QBrush(QColor(255, 255, 0))) newItem.setBackground(QBrush(QColor(0, 0, 255))) tableWidget.setItem(0, 1, newItem) newItem = QTableWidgetItem('160') newItem.setFont(QFont('times', 20, QFont.Black)) newItem.setForeground(QBrush(QColor(0, 0, 255))) tableWidget.setItem(0, 2, newItem) self.setLayout(layout) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) main = CellFontAndColor() main.show() sys.exit(app.exec_())
每天更新pyQt5内容