在 QTableWidget 表格中设置单元格的字体和颜色 的案例
import sys
from PyQt5 import QtPrintSupport, QtGui
from PyQt5.QtCore import Qt, QMimeData, QDate, QDateTime, QTime, QStringListModel
from PyQt5.QtGui import QIcon, QPainter, QBrush, QPixmap, QStandardItemModel, QStandardItem, QColor, QFont
from PyQt5.QtPrintSupport import QPageSetupDialog, QPrinter, QPrintDialog
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QFormLayout, QLabel, QLineEdit, QPushButton, QGridLayout, \
QCalendarWidget, QVBoxLayout, QDateTimeEdit, QAction, QMainWindow, QTextEdit, QStatusBar, QFileDialog, QDialog, \
QTableView, QMessageBox, QListView, QListWidget, QHBoxLayout, QTableWidget, QTableWidgetItem, QAbstractItemView
'''
在 QTableWidget 表格中设置单元格的字体和颜色 的案例
'''
class CellFontAndColorDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 460, 360)
# 设置窗口标题
self.setWindowTitle('QTableWidget扩展表格的单元格放置控件 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
layout = QHBoxLayout()
tablewidget = QTableWidget()
tablewidget.setRowCount(4)
tablewidget.setColumnCount(3)
layout.addWidget(tablewidget)
tablewidget.setHorizontalHeaderLabels(['姓名','性别','体重(kg)'])
newItem = QTableWidgetItem('玉皇大帝')
newItem.setFont(QFont('微软雅黑',14,QFont.Black))
newItem.setForeground(QBrush(QColor(190,88,0)))
tablewidget.setItem(0,0,newItem)
newItem = QTableWidgetItem('维纳斯')
newItem.setFont(QFont('宋体',14,QFont.Black))
newItem.setForeground(QBrush(QColor(190,0,0)))
newItem.setBackground(QBrush(QColor(0,220,220)))
tablewidget.setItem(0,1,newItem)
newItem = QTableWidgetItem('180')
newItem.setFont(QFont('宋体',28,QFont.Black))
newItem.setForeground(QBrush(QColor(0,120,255)))
# newItem.setBackground(QBrush(QColor(0,220,0)))
tablewidget.setItem(0,2,newItem)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = CellFontAndColorDemo()
w.show()
sys.exit(app.exec_())