I need to randomly fill out the table. I did this using the for loop. But filling occurs only at the last iteration. That is, only one cell is repainted. This is a function:
def generateTable(self):
count = random.randint(10, 20)
item = QtWidgets.QTableWidgetItem()
predator = QtGui.QBrush(QtGui.QColor(0, 255, 0))
predator.setStyle(QtCore.Qt.SolidPattern)
herbivore = QtGui.QBrush(QtGui.QColor(255, 0, 0))
herbivore.setStyle(QtCore.Qt.SolidPattern)
for i in range(count):
x = random.randint(0, 19)
y = random.randint(0, 29)
if random.randint(0, 1) == 0:
item.setBackground(predator)
self.BoardTable.setItem(x, y, item)
else:
item.setBackground(herbivore)
self.BoardTable.setItem(x, y, item)
I think there is not enough of an important line in the loop.
I had to put QTableWidgetItem in the loop:
def generateTable(self):
count = random.randint(10, 20)
predator = QtGui.QBrush(QtGui.QColor(255, 0, 0))
predator.setStyle(QtCore.Qt.SolidPattern)
herbivore = QtGui.QBrush(QtGui.QColor(0, 255, 0))
herbivore.setStyle(QtCore.Qt.SolidPattern)
for i in range(count):
item = QtWidgets.QTableWidgetItem()
x = random.randint(0, 19)
y = random.randint(0, 29)
if random.randint(0, 1) == 0:
item.setBackground(predator)
self.BoardTable.setItem(x, y, item)
else:
item.setBackground(herbivore)
self.BoardTable.setItem(x, y, item)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.