1

In the App there are a QButton and a QLabel. In the QLabel I put a QMovie in, to show a GIF. By clicking the QButton I want to change the GIF, which path is defined in a list.

The problem: the App shows only the first GIF. The Button seems not working. What have I done wrong?

But: Please dont change the structure of the code. E.g. I want to have the QLabel defined in the sub-function and return from there the QLabel.

The code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import random

list = ['F:\\test1.gif', 'F:\\test2.gif', 'F:\\test3.gif', 'F:\\test4.gif']

class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.resize(600, 600)
        self.initUI()

    def initUI(self):
        self.btn = QPushButton("change", self)
        self.btn.clicked.connect(self.changeGIF)

        self.grid = QVBoxLayout()
        self.grid.addWidget(self.btn)
        self.grid.addWidget(self.changeGIF())
        self.grid.addStretch(1)

        self.setLayout(self.grid)


    def changeGIF(self):
        randomValue = list[random.randint(1, len(list)-1)]
        print(randomValue)

        self.lbl = QLabel()
        self.gif = QMovie(randomValue)
        self.lbl.setMovie(self.gif)
        self.gif.start()

        return self.lbl


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MyApp = Window()
    MyApp.show()
    sys.exit(app.exec_())

Thanks for the help!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Rt Rtt
  • 595
  • 2
  • 13
  • 33
  • Why do you want that function to return the QLabel? – eyllanesc Jan 18 '18 at 07:07
  • @eyllanesc I actually want to use this line: self.grid.addWidget(self.changeGIF()) It helps me a lot, because I have a lot of sub-functions. – Rt Rtt Jan 18 '18 at 07:09
  • That method you're only calling it in the initUI once, and then you never call it, plus that method would add a new QLabel. – eyllanesc Jan 18 '18 at 07:11
  • And depending on what you want, you want to replace the QMovie, you do not want to add a new QLabel. What is your goal, use addWidget() or change the GIF? – eyllanesc Jan 18 '18 at 07:12
  • @eyllanesc That is what I want to have, or what I have tried. I want to change/re-define the QLabel everytime, when the button is clicked. – Rt Rtt Jan 18 '18 at 07:12
  • @eyllanesc Everytime when the button is clicked, the Label is re-defined, thus everything changes. That is what I thought. – Rt Rtt Jan 18 '18 at 07:14
  • when using addWidget () you are adding another QLabel (), that is, every time you use addWidget () there will be a new QLabel. Do you want another GIF to be added or do you want to replace it? – eyllanesc Jan 18 '18 at 07:14
  • 1
    No, you're wrong, addWidget add another widget, it does not redefine it. – eyllanesc Jan 18 '18 at 07:14
  • @eyllanesc Oh, I didnot know about that. Is there any method to redefine the QLable everytime for every click? – Rt Rtt Jan 18 '18 at 07:16
  • 1
    You only have to reuse the same QLabel, but for this it is not necessary to use addWidget() so if I publish a response I will change the structure of your code. Do you agree with it? – eyllanesc Jan 18 '18 at 07:18
  • @eyllanesc That would be allright for me! – Rt Rtt Jan 18 '18 at 07:18
  • Try my answer.. – eyllanesc Jan 18 '18 at 07:34

1 Answers1

2

since the QLabel will be responsible for showing GIFs in a random way, it is advisable to create a class that only takes care of that task, in this widget you must have a method that changes the QMovie of the QLabel.

list_of_gifs = ['F:\\test1.gif', 'F:\\test2.gif', 'F:\\test3.gif', 'F:\\test4.gif']

class GIFLabel(QLabel):
    def __init__(self, gifs, *args, **kwargs):
        QLabel.__init__(self, *args, **kwargs)
        self.mGifs = gifs
        self.changeGIF()

    def changeGIF(self):
        gif = random.choice(self.mGifs)
        movie = QMovie(gif)
        self.setMovie(movie)
        movie.start()

class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.resize(600, 600)
        self.initUI()

    def initUI(self):
        self.btn = QPushButton("change", self)
        self.label = GIFLabel(list_of_gifs, self)
        self.btn.clicked.connect(self.label.changeGIF)
        self.grid = QVBoxLayout(self)
        self.grid.addWidget(self.btn)
        self.grid.addWidget(self.label)
        self.grid.addStretch(1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MyApp = Window()
    MyApp.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241