PyQT 动态地自动更改 table 小部件的颜色属性

PyQT dynamically, automatically changing color properties of table widget

由于我是 PyQt 的新手,我正在尝试通过动态更改 table 单元格的颜色来解决问题 - 非常欢迎帮助。

Function testFunction should change tableWidget color if for loop find 1 in array or 0.是否可以设置这个属性?盒子应该每 2 秒自动改变颜色,无需任何额外操作。检查下面的代码...

import sys, os
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QWidget):

    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle("Hello!")
        self.tableWidget = QtGui.QTableWidget()
        self.tableItem = QtGui.QTableWidgetItem()
        self.tableWidget.resize(400,250)
        self.tableWidget.setRowCount(1)
        self.tableWidget.setColumnCount(1)
        self.tableWidget.setItem(0,0, QtGui.QTableWidgetItem("START TOOL"))
        self.tableWidget.item(0,0).setBackground(QtGui.QColor(100,100,150))

        realLayout = QtGui.QVBoxLayout()
        realLayout.addWidget(tableWidget)
        self.setLayout(realLayout)
        self.testFunction()

    def testFunction(self) :
        a = [1,0,1,1,1,1,1,0,0,0,0,0,1]
        for i in range(0,len(a)) :
            if a[i] == 1 :
                self.tableWidget.item(0,0).setBackground(QtGui.QColor(100,100,100))
            else :
                self.tableWidget.item(0,0).setBackground(QtGui.QColor(0,255,0))    
            time.sleep(2)

def main():
    app = QtGui.QApplication(sys.argv)
    GUI = MainWindow()
    GUI.show()
    sys.exit(app.exec_())

if __name__ == '__main__' :
    main()

您必须将 realLayout.addWidget(tableWidget) 更改为 realLayout.addWidget(self.tableWidget) 并且您不应使用 "sleep",必须创建一个计时器(QTimer)

import sys, os
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QTimer


class MainWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle("Hello!")
        self.tableWidget = QtGui.QTableWidget()
        self.tableItem = QtGui.QTableWidgetItem()
        self.tableWidget.resize(400, 250)
        self.tableWidget.setRowCount(1)
        self.tableWidget.setColumnCount(1)
        self.tableWidget.setItem(0, 0, QtGui.QTableWidgetItem("START TOOL"))
        self.tableWidget.item(0, 0).setBackground(QtGui.QColor(100, 100, 150))

        realLayout = QtGui.QVBoxLayout()
        realLayout.addWidget(self.tableWidget)
        self.setLayout(realLayout)
        self.counter = 0
        self.a = [1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1]
        self.testFunction()

        timer = QTimer(self)
        timer.timeout.connect(self.testFunction)
        timer.start(2*1000)

    def testFunction(self):
        self.counter += 1
        self.counter %= len(self.a)
        if self.a[self.counter]:
            self.tableWidget.item(0, 0).setBackground(QtGui.QColor(100, 100, 100))
        else:
            self.tableWidget.item(0, 0).setBackground(QtGui.QColor(0, 255, 0))


def main():
    app = QtGui.QApplication(sys.argv)
    GUI = MainWindow()
    GUI.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()