pyqt tablewidget改变颜色
时间: 2023-08-28 09:08:13 浏览: 167
要改变PyQt中TableWidget的单元格颜色,可以使用setItem()函数将单元格的背景色设置为所需颜色。以下是一个简单的例子:
```python
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem
class Example(QTableWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('TableWidget Background')
self.setRowCount(3)
self.setColumnCount(3)
# 设置第二行第二列单元格的背景色为红色
item = QTableWidgetItem('Red Cell')
item.setBackground(QColor(255, 0, 0))
self.setItem(1, 1, item)
self.show()
if __name__ == '__main__':
app = QApplication([])
ex = Example()
app.exec_()
```
在上面的例子中,我们首先创建了一个3x3的表格,然后使用QTableWidgetItem创建一个文本单元格。然后,我们使用setBackground()函数将第二行第二列单元格的背景色设置为红色。最后,我们使用setItem()函数将这个单元格添加到表格中。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)