1

I am adding a gif to my QMainWindow. I want the size of my window to match exactly the size of QMovie but my QMovie is truncated at the first place and even if use resize on that, its still not showing fully.

Here is the code :

from typing import Text
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
import sys

class MainWindow(qtw.QDialog):
    
    def __init__(self, *arg, **kwargs):
        super().__init__(*arg, **kwargs)
        
        self.centralwidget = qtw.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")

        self.label = qtw.QLabel(self.centralwidget)       

        movie = qtg.QMovie('mic_listen2.gif')
        movie.setScaledSize(qtc.QSize(50,50))

        self.label.setMovie(movie)
        
        movie.start()
        self.show()
    
if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())

Here is the gif

https://media.giphy.com/media/QGMXK7Byy6MSXKVRlc/giphy.gif

Here is my output

enter image description here

H Kumar
  • 37
  • 6

1 Answers1

1

You must set both the minimum size of the label and that of the widget.

enter image description here

#!/usr/bin/env python3
from typing import Text
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys


class MainWindow(QtWidgets.QDialog):
    def __init__(self, movsize=50, *arg, **kwargs):
        super().__init__(*arg, **kwargs)

        self.centralwidget = QtWidgets.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")

        self.label = QtWidgets.QLabel(self.centralwidget)

        self.movie = QtGui.QMovie("giphy.gif")
        self.movie.setScaledSize(QtCore.QSize(movsize, movsize))
        self.movie.start()

        self.label.setMovie(self.movie)
        self.label.setMinimumSize(self.movie.scaledSize())
        self.centralwidget.setMinimumSize(self.label.minimumSize())
        # Uncomment to force the window to take the size of the movie.
        # self.setMaximumSize(self.movie.scaledSize())
        self.show()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w2 = MainWindow(movsize=160)
    w3 = MainWindow(movsize=500)
    sys.exit(app.exec_())
Fredrick Brennan
  • 7,079
  • 2
  • 30
  • 61
  • great, that works for me, could you also help me in resizing the main window as the size of my gif. SO that there is no whitespace left right or bottom top – H Kumar Oct 22 '22 at 12:32