gpt4 book ai didi

python - 为什么 QTableWidgetItem 不不断更新它的颜色?

转载 作者:行者123 更新时间:2023-12-01 01:28:21 28 4
gpt4 key购买 nike

我的程序中有一个 QTableWidget。我希望它的行和列根据表格小部件中的时间条目改变颜色。当我第一次添加时间时,它将与当前系统时间进行比较,并且颜色更改确实生效,但随着时间的推移,它不会跟上并且颜色保持不变。我希望它不断更新表格项目的颜色。我无法包含所有代码,因为它太长了,但我已经包含了相关部分。

#Operations_Tree is the QTABLEWIDGET
def Scheduler_Loop(self):
Timer = QtCore.QTimer(self)
Timer.start(1000)
Timer.timeout.connect(self.test)

def test(self):

time = QtCore.QTime.currentTime()
current_time_text = time.toString('hh:mm:ss')
for row in range(0,self.Operations_Tree.rowCount()):
time = self.Operations_Tree.item(row,1)
time_text = time.text()
#print(time_text)
if time_text >= current_time_text :
for column in range(0,6):
#print("TEST")
try:
table_widget_item = self.Operations_Tree.item(row, column)
table_widget_item.setForeground(QtGui.QColor(0,0,255)) #BLue
table_widget_item.setBackground(QtGui.QColor(238,233,233)) #
table_widget_item.viewport.update()
except AttributeError:
pass
else:
for column in range(0,6):
#print("TEST")
try:
table_widget_item = self.Operations_Tree.item(row, column)
#table_widget_item.setForeground(QtGui.QColor(0,255,0)) #
table_widget_item.setBackground(QtGui.QColor(238,233,233)) #
table_widget_item.viewport.update()
except AttributeError:
pass
self.Operations_Tree.update()

这是我的程序的快照:

enter image description here

最佳答案

对我来说,主要问题是你比较的是字符串而不是时间,而且在 Qt 中也没有必要使用 try- except,因为很多时候它们会隐藏错误并浪费不必要的资源,最好进行验证。

PyQt5:

from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.lcd_number = QtWidgets.QLCDNumber()
self.lcd_number.setFrameShape(QtWidgets.QFrame.NoFrame)
self.lcd_number.setFixedHeight(100)
self.lcd_number.setDigitCount(8)
self.Operations_Tree = QtWidgets.QTableWidget()
self.Operations_Tree.setColumnCount(6)
labels = ["On AIR", "TIME", "ITEM", "DURATION", "B.ITEM", "B.I.DURATION"]
self.Operations_Tree.setHorizontalHeaderLabels(labels)

lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.lcd_number)
lay.addWidget(self.Operations_Tree)

for i, t in enumerate(("10:10:10", "12:12:12")):
self.Operations_Tree.insertRow(self.Operations_Tree.rowCount())
it = QtWidgets.QTableWidgetItem(t)
self.Operations_Tree.setItem(i, 1, it)

self.resize(640, 480)
self.Scheduler_Loop()

def Scheduler_Loop(self):
Timer = QtCore.QTimer(self)
Timer.timeout.connect(self.test)
Timer.start(1000)
self.test()

@QtCore.pyqtSlot()
def test(self):
time = QtCore.QTime.currentTime()
self.lcd_number.display(time.toString())
for row in range(self.Operations_Tree.rowCount()):
it = self.Operations_Tree.item(row, 1)
r_time = QtCore.QTime.fromString(it.text(), "h:mm:ss")
flag = r_time >= time
f_color = QtGui.QColor(0, 0, 255) if flag else QtGui.QColor(0, 255, 0)
b_color = QtGui.QColor(238, 233, 233) if flag else QtGui.QColor(238,233,233)
for column in range(0, 6):
it = self.Operations_Tree.item(row, column)
if it is None:
it = QtWidgets.QTableWidgetItem()
self.Operations_Tree.setItem(row, column, it)
it.setForeground(f_color)
it.setBackground(b_color)

if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

PyQt4:

from PyQt4 import QtCore, QtGui


class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.lcd_number = QtGui.QLCDNumber()
self.lcd_number.setFrameShape(QtGui.QFrame.NoFrame)
self.lcd_number.setFixedHeight(100)
self.lcd_number.setDigitCount(8)
self.Operations_Tree = QtGui.QTableWidget()
self.Operations_Tree.setColumnCount(6)
labels = ["On AIR", "TIME", "ITEM", "DURATION", "B.ITEM", "B.I.DURATION"]
self.Operations_Tree.setHorizontalHeaderLabels(labels)

lay = QtGui.QVBoxLayout(self)
lay.addWidget(self.lcd_number)
lay.addWidget(self.Operations_Tree)

for i, t in enumerate(("10:10:10", "12:12:12")):
self.Operations_Tree.insertRow(self.Operations_Tree.rowCount())
it = QtGui.QTableWidgetItem(t)
self.Operations_Tree.setItem(i, 1, it)

self.resize(640, 480)
self.Scheduler_Loop()

def Scheduler_Loop(self):
Timer = QtCore.QTimer(self)
Timer.timeout.connect(self.test)
Timer.start(1000)
self.test()

@QtCore.pyqtSlot()
def test(self):
time = QtCore.QTime.currentTime()
self.lcd_number.display(time.toString())
for row in range(self.Operations_Tree.rowCount()):
it = self.Operations_Tree.item(row, 1)
r_time = QtCore.QTime.fromString(it.text(), "h:mm:ss")
flag = r_time >= time
f_color = QtGui.QColor(0, 0, 255) if flag else QtGui.QColor(0, 255, 0)
b_color = QtGui.QColor(238, 233, 233) if flag else QtGui.QColor(238,233,233)
for column in range(0, 6):
it = self.Operations_Tree.item(row, column)
if it is None:
it = QtGui.QTableWidgetItem()
self.Operations_Tree.setItem(row, column, it)
it.setForeground(f_color)
it.setBackground(b_color)

if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

关于python - 为什么 QTableWidgetItem 不不断更新它的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138605/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com