氣輕PyQt6 32 表-背景顏色文字顏色
?
單元背景顏色和文字顏色的處理執(zhí)行后都無法立刻顯示,update(),viewport().update()以及repaint()等處理都試過,但都沒效果。
?
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QBrush,QColor,QFont
import sys
?
treat = ['背景顏色','文字顏色']
class PyQt632(QWidget):
??? def __init__(self):
??????? super().__init__()
??????? self.initUI()
??? def initUI(self):
??????? self.setWindowTitle('氣輕PyQt6')??????????????? # 設(shè)置窗口標(biāo)題
??????? self.resize(490, 300)?????????????? ????????????# 設(shè)置窗口大小
??????? self.setStyleSheet('background-color:#FFC1C1')
?
??????? self.bgColor = QPushButton(treat[0], self)
??????? self.bgColor.setGeometry(10, 120, 80, 30)
??????? self.bgColor.setStyleSheet('background-color:#F5DEB3;color : #8B8682; \
??????????????????? font: bold large /"SimSun/";font-size:16px')
??????? self.bgColor.clicked.connect(lambda :self.buttonClicked(self.bgColor))
?
??????? self.textColor = QPushButton(treat[1], self)
??????? self.textColor.setGeometry(10, 150, 80, 30)
??? ????self.textColor.setStyleSheet('background-color:#F5DEB3;color : #8B8682; \
??????????????????? font: bold large /"SimSun/";font-size:16px')
??????? self.textColor.clicked.connect(lambda :self.buttonClicked(self.textColor))
?
??????? titles = ['編號', ' 第一列 ', ' 第二列 ', ' 第三列 ', ' 第四列 ']
??????? self.table = QTableWidget(8,5,self)???????????? # 創(chuàng)n行m列建空表格
??????? self.table.setGeometry(100, 60,380,200)???????? # 設(shè)置位置和大小
??????? self.table.setStyleSheet('background-color:#E6E6FA;color : #696969; \
???????????? ???????font: bold italic large /"Times New Roman/";font-size:16px')
??????? self.table.setHorizontalHeaderLabels(titles)??? # 水平標(biāo)題
??????? vtitle = list(map(str, list(range(8))))
??????? self.table.setVerticalHeaderLabels(vtitle)????? # 垂直標(biāo)題
????????????? ??????????????????????????????????????????# 自動調(diào)整列寬
??????? self.table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
?
??????? self.table.setAlternatingRowColors(True)??????? # 行自動變色
??????? self.table.setFont(QFont("宋體",20))
?
??????? item=QTableWidgetItem('氣輕')
??????? self.table.setItem(0,0, item)
???????
??????? item=QTableWidgetItem('氣輕python')
??????? self.table.setItem(2,0, item)
???????
??????? item=QTableWidgetItem('氣輕PyQt6')
??????? self.table.setItem(4,0, item)
???????
??????? self.show()
?
??? def buttonClicked(self,b):
??????? action = b.text()
??????? rno = self.table.currentRow()
??????? cno = self.table.currentColumn()
?
??????? if rno == -1 or? cno== -1:????????????????????? # 無效位置?
??????????? reply = QMessageBox()
??????????? reply.setText('請選擇有效位置')
??????????? reply.setStandardButtons(QMessageBox.StandardButton.Yes)
??????????? x = reply.exec()
??????????? return
?
??????? reply = QMessageBox()
?
??????? col = QColorDialog.getColor()
?
??????? if not col.isValid():
??????????? reply = QMessageBox()
??????????? reply.setText('請選擇有效顏色')
??????????? reply.setStandardButtons(QMessageBox.StandardButton.Yes)
??????????? x = reply.exec()
??????????? return
?
??????? func = [self.table.item(rno,cno).setBackground,
??????????????? self.table.item(rno,cno).setForeground]
??????? fdict = dict(zip(treat,func))
??????? fdict[action](QBrush(QColor(col.name())))
?
if __name__ == '__main__':
??? app = QApplication(sys.argv)
??? window = PyQt632()
??? sys.exit(app.exec())
?
執(zhí)行結(jié)果
