0

I want to change the image of the label many times.

But, only the code written in "if, elif" can change the image of the label.

Why does this problem appear? How to fix this problem?

Here is my code:

self.DrawLotsButton.clicked.connect(self.DL)

def DL(self):
    awnser = DrawLots.MainLot()
    self.qPixmapFileVar = QPixmap()
    self.qPixmapFileVar.load("Things/Dugudugu.jpeg") #There is a problem with this part
    self.qPixmapFileVar = self.qPixmapFileVar.scaledToWidth(418) #There is a problem with this part
    self.qPixmapFileVar = self.qPixmapFileVar.scaledToHeight(475)#There is a problem with this part
    self.A_Label.setPixmap(self.qPixmapFileVar)#There is a problem with this part
    playsound.playsound("Dugudugu_sound.mp3", False)
    time.sleep(3.5)
    if awnser == "꽝":
        self.qPixmapFileVar = QPixmap()
        self.qPixmapFileVar.load("Things/GGwang.png")
        self.qPixmapFileVar = self.qPixmapFileVar.scaledToWidth(418)
        self.qPixmapFileVar = self.qPixmapFileVar.scaledToHeight(475)
        self.A_Label.setPixmap(self.qPixmapFileVar)
    elif awnser == "2000원":
        self.qPixmapFileVar = QPixmap()
        self.qPixmapFileVar.load("Things/2000.png")
        self.qPixmapFileVar = self.qPixmapFileVar.scaledToWidth(418)
        self.qPixmapFileVar = self.qPixmapFileVar.scaledToHeight(475)
        self.A_Label.setPixmap(self.qPixmapFileVar)
    elif awnser == "3000원":
        self.qPixmapFileVar = QPixmap()
        self.qPixmapFileVar.load("Things/3000.png")
        self.qPixmapFileVar = self.qPixmapFileVar.scaledToWidth(418)
        self.qPixmapFileVar = self.qPixmapFileVar.scaledToHeight(475)
        self.A_Label.setPixmap(self.qPixmapFileVar)
   
OverNaver
  • 21
  • 1
  • The problem is the same as your other question: you're using a blocking function (`time.sleep`). Qt, like all UI toolkits, is [*event driven*](https://en.wikipedia.org/wiki/Event-driven_programming), which means that its event loop must be able to process events without blocking. Just like your other question (where `playsound()` blocked the UI), `time.sleep` doesn't allow the UI to properly update itself, so you can only see the "last" pixmap set. Use `QTimer.singleShot(3500, lambda: self.A_Label.setPixmap(self.qPixmapFileVar))`. – musicamante Apr 20 '22 at 14:17
  • Also: 1. just create the pixmap with the path: `self.qPixmapFileVar = QPixmap('image.png')`; 2. don't scale the image in two steps, instead use [`scaled()`](https://doc.qt.io/qt-5/qpixmap.html#scaled) and apply a smooth transformation: `self.qPixmapFileVar.scaled(418, 475, Qt.KeepAspectRatio, Qt.SmoothTransformation)`; 3. try to improve your code: if you have the same function calls in all `if/elif/else` block, do it just once after the end of that block, instead of repeating them every single time. – musicamante Apr 20 '22 at 14:25

0 Answers0