Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help with Gif



  • Hii. Someone can help me to put a Gif in my base code? The gif need to be seet size (100,100)..

    from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
        QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
    from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
        QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
        QPixmap, QRadialGradient, QMovie)
    from PySide2.QtWidgets import *
    import sys 
    
    class Base(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent) 
    
    if __name__ == '__main__':
        qt = QApplication(sys.argv)
        app = Base() 
        app.show()  
        qt.exec_()  
    

  • Banned

    @Julie-Db here is a PyQt5 MUC that I shared with someone else on here recently all you need to do is change the PyQt5 reference to PySide2 and change the pyqtSlot reference to just Slot as I believe PySide2 supports everything else. However if you are not creating a commercial product you might want to switch to PyQt5 it is more up to date than PySide2

    from PyQt5.QtCore    import pyqtSlot
    from PyQt5.QtGui     import QMovie
    from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QVBoxLayout
    from PyQt5.QtWidgets import QWidget, QFrame, QLabel, QPushButton
    
    class GifFrame(QLabel):
        def __init__(self, parent):
            QLabel.__init__(self)
            self.Parent = parent
    
            self.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
            self.setLineWidth(10)
            self.setMidLineWidth(10)
            self.setStyleSheet('background-color: rgb(255, 0, 0)')
    
            self.DaGif = QMovie("C:/Images/Gifs/giphy.gif")
            
            self.lblGifHldr = QLabel()
            self.lblGifHldr.setMovie(self.DaGif)
    
            self.DaGif.start()
            self.Parent.IsOn = True
    
            HBox = QHBoxLayout()
            HBox.addStretch(1)
            HBox.addWidget(self.lblGifHldr)
            HBox.addStretch(1)
            
            self.setLayout(HBox)
            
    class CenterPanel(QWidget):
        def __init__(self, parent):
            QWidget.__init__(self)
            self.Parent = parent
            self.IsOn = False
    
            self.btnQuit = QPushButton('Stop')
            self.btnQuit.clicked.connect(self.ToggleIt)
    
            HBox = QHBoxLayout()
            HBox.addStretch(1)
            HBox.addWidget(self.btnQuit)
    
            self.GifView = GifFrame(self)
    
            VBox = QVBoxLayout()
            VBox.addWidget(self.GifView)
            VBox.addLayout(HBox)
            
            self.setLayout(VBox)
    
        @pyqtSlot()
        def ToggleIt(self):
            if self.IsOn:
                self.IsOn = False
                self.btnQuit.setText('Start')
                self.GifView.DaGif.stop()
            else:
                self.IsOn = True
                self.btnQuit.setText('Stop')
                self.GifView.DaGif.start()
    
    class MainUI(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            self.setWindowTitle('Framed It')
            
            WinLeft = 550; WinTop = 250; WinWidth = 550; WinHigh = 550
            self.setGeometry(WinLeft, WinTop, WinWidth, WinHigh)
    
            self.CenterPane = CenterPanel(self)
            self.setCentralWidget(self.CenterPane)
    
    if __name__ == "__main__":
        MainEventHandler = QApplication([])
    
        MainApplication = MainUI()
        MainApplication.show()
        
        MainEventHandler.exec()
    


  • If you use PyQt you can use QMovie to play (animated) gifs.
    Like this:

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtGui import QMovie
    import sys
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(250, 250)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
    
            # create label
            self.label = QtWidgets.QLabel(self.centralwidget)
            self.label.setGeometry(QtCore.QRect(25, 25, 200, 200))
            self.label.setMinimumSize(QtCore.QSize(200, 200))
            self.label.setMaximumSize(QtCore.QSize(200, 200))
            self.label.setObjectName("label")
    
            # add label to main window
            MainWindow.setCentralWidget(self.centralwidget)
    
            # set qmovie as label
            self.movie = QMovie("earth.gif")
            self.label.setMovie(self.movie)
            self.movie.start()
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(window)
        window.show()
        sys.exit(app.exec_())
    
    

    source: https://pythonpyqt.com/pyqt-gif/


Log in to reply