0

Im trying to use a gif to show that its loading.Then Function only runs once I press on a button in the GUI. However, when the function starts loading it shows an empty window and the gif only appears after the the QMessage box appears notifying that it is done.

How do I make it so that it will the gif will appear when it starts and closes once the function finishes loading. Thanks

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.uic import loadUi
from tkinter import messagebox
import os
import datetime
import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal, QObject, pyqtSlot,Qt
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QProgressBar, QVBoxLayout,QMessageBox,QLabel
from PyQt5.QtGui import QMovie


class Ui_MainWindow(QtWidgets.QMainWindow):
    

    def __init__(self):
        super(Ui_MainWindow, self).__init__()

        loadUi("PBtest.ui", self)
        self.resize(1500, 945)
        
        self.setWindowTitle("MySQL Filter")
        self.load = LoadingGif()

        self.pushButton.clicked.connect(self.start)  



    def start(self):

        self.load.show()
        
        time.sleep(4)
        
        self.load.close()


        QMessageBox.information(self, "Message", "Data Loaded")

class LoadingGif(QWidget):
  
    def __init__(self):
        super().__init__()
        self.setFixedSize(500,500)
        #self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.CustomizeWindowHint)
        self.setWindowTitle('Loading')
        self.label=QLabel(self)

  
        # Loading the GIF
        self.movie = QMovie('loader.gif')
        self.label.setMovie(self.movie)
  
        self.startAnimation()
        
  
    # Start Animation
  
    def startAnimation(self):
        self.movie.start()
  
    # Stop Animation(According to need)
    def stopAnimation(self):
        self.movie.stop()



if __name__ == '__main__':

    app = QApplication(sys.argv) #to start the GUI
    window = Ui_MainWindow() #To initalize the window
    window.show() #show
    sys.exit(app.exec_())

Dyan
  • 1
  • 2
    `time.sleep` and long running procedures freeze the GUI and keep it from updating. You should either take advantage of QTimer, or run your code in a separate thread. Also you shouldn't mix tkinter and PyQt. – Alexander Feb 06 '23 at 08:16

0 Answers0