PDA

View Full Version : How to create a matrix of Qlabels



Lugi
24th December 2020, 18:43
I want to create somekind of deteriorating shields for my college project, so I went with this kind of implementation. But for some reason when I start my game it completly freeze's. Is it because I didn't write my code in a correct way? Can someone try to correct it.


I have a class, looking like this:

from PyQt5.QtWidgets import QLabel

col = 14
row = 8


class ShieldLabel:
def __init__(self, pix, x, y, width, height):
self.livingObjects = [row][col]

for i in range(0, row):
for j in range(0, col):
self.shieldPart = QLabel()
self.shieldPart.setPixmap(pix)
self.shieldPart.setGeometry(x + (i * 8), y + (j * 8), width, height)
self.setStyleSheet("background:transparent")

self.livingObjects[i, j] = self.shieldPart

And in my main I call it like this: (it's in a loop because I want 4 objects of 2d array from my class above)
def show_shields(self):
for i in range(0, 4):
new_shield = ShieldLabel(QPixmap("Images/shieldPart.png"), 120 + (i * 300), 480, 80, 80)
self.shieldArray.append(new_shield)

ChrisW67
27th December 2020, 00:23
I want to create somekind of deteriorating shields
really do not know what this means

for my college project, so I went with this kind of implementation. But for some reason when I start my game it completly freeze's.
You will need to define freezes. The code you have posted is not complete enough to run and nothing there should "freeze."

Is it because I didn't write my code in a correct way?
Maybe. At a glance your ShieldLabel objects will each contain 14x8 label object with a notional 80x80 size but absolutely positioned on an 8x8 spacing, i.e. mostly overlapping. Not sure if that is what you intended. Nothing actually displays these widgets in the code we see. If you simply construct these structures but never arrange from them to be shown before calling app.exec() you'll get no GUI but the event loop will be running (perhaps that is your freezes).

Your code in
block


from PyQt5.QtWidgets import QLabel

col = 14
row = 8


class ShieldLabel:
def __init__(self, pix, x, y, width, height):
self.livingObjects = [row][col]

for i in range(0, row):
for j in range(0, col):
self.shieldPart = QLabel()
self.shieldPart.setPixmap(pix)
self.shieldPart.setGeometry(x + (i * 8), y + (j * 8), width, height)
self.setStyleSheet("background:transparent")

self.livingObjects[i, j] = self.shieldPart

And in my main I call it like this: (it's in a loop because I want 4 objects of 2d array from my class above)


def show_shields(self):
for i in range(0, 4):
new_shield = ShieldLabel(QPixmap("Images/shieldPart.png"), 120 + (i * 300), 480, 80, 80)
self.shieldArray.append(new_shield)