I'm trying to recreate minesweeper in python with PyQT5.
I have this function that updates the GUI by using nested for loops (one for column, and one for row) to check each cell in a 19x19 area to determine what type of tile said cell is.
The function works as intended, but runs very slow and takes around 4 seconds to complete. I have tried using threading, but this further harms performance.
Here is the code:
(I know it's not the best, it's still a prototype)
def update_display(self):
if(Debug == 1):
print("[Updating Cells...]")
for column in range(self.cells):
#print(column)
for row in range(self.cells):
#print(row)
#if(Debug == 1):
# print("[Changing tile ({},{})]".format(row,column))
if self.initialArray[row][column] <= 9:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_plain.gif'))
elif self.initialArray[row][column] == 10:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_clicked.gif'))
elif self.initialArray[row][column] == 11:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_1.gif'))
elif self.initialArray[row][column] == 12:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_2.gif'))
elif self.initialArray[row][column] == 13:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_3.gif'))
elif self.initialArray[row][column] == 14:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_4.gif'))
elif self.initialArray[row][column] == 15:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_5.gif'))
elif self.initialArray[row][column] == 16:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_6.gif'))
elif self.initialArray[row][column] == 19:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_flag.gif'))
elif self.initialArray[row][column] == 29:
self.button[row, column].setIcon(QtGui.QIcon(dataFolder + 'tile_mine.gif'))
Is there a faster way to do this? (Preferably < 1 second as it needs to draw each cell)
If it helps any, I am using Python 3.10
dataFile[ self.initialArray[row][column] ]
. Your problem is callingsetIcon(QtGui.QIcon(path))
389 times. I don't know enough about QTGui or your code'ssetIcon
to help further than that. \$\endgroup\$