I'm working on a program that changes the GIF based on which function is currently active. It's like a voice assistant type of program. Basically what I want is that when I run the program GIF1 is displayed. The program waits for user's input and when input is given, program changes display to GIF2. After the function has finished the work and goes back to "waiting for input" stage then GIF1 is displayed again instead of GIF2. The problem is that I'm only about a week into learning PyQt5. What I've done so far is a program that changes the GIF with button click. I tried to implement threading but it didn't helped. I tried calling the specific function from class that changes the GIF, but the GIF only stays the same and input loop is frozen. If I remove the button at all it shows nothing and input still does nothing, but it doesn't freeze. How can I fix this? What type of threading should I use?
! Note - for now I only use basic input to actually change the GIF. As I mentioned after the input is given the GIF should change to GIF2 and when the program goes back to waiting for input the GIF2 should change to GIF1 again.
Here is the code:
# Libraries:
import sys
# Modules:
from PyQt5 import QtGui
from PyQt5.QtGui import QMovie
from PyQt5.QtWidgets import QApplication, QDesktopWidget, QLabel, QPushButton, QVBoxLayout, QWidget
# Main Window Object:
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(800, 600)
self.setMainWindow()
def setMainWindow(self):
self.setWindowIcon(QtGui.QIcon("GUI/Icon.png"))
self.setWindowTitle("GIF Display")
self.centerMainWindow()
self.button()
def button(self):
self.testButton = QPushButton("Test!", self)
self.label = GIFChanger(self)
self.testButton.clicked.connect(self.label.changeToSpeech)
self.grid = QVBoxLayout(self)
self.grid.addWidget(self.testButton)
self.grid.addWidget(self.label)
self.grid.addStretch(1)
def centerMainWindow(self):
mainWindow = self.frameGeometry()
dekstopCenter = QDesktopWidget().availableGeometry().center()
mainWindow.moveCenter(dekstopCenter)
self.move(mainWindow.topLeft())
class GIFChanger(QLabel):
def __init__(self, *args, **kwargs):
QLabel.__init__(self, *args, **kwargs)
self.changeToSilence()
def changeToSpeech(self):
mainMode = QMovie("GUI/GIF1.gif")
self.setMovie(mainMode)
mainMode.start()
def changeToSilence(self):
silenceMode = QMovie("GUI/GIF2.gif")
self.setMovie(silenceMode)
silenceMode.start()
if __name__ == "__main__":
MyApp = QApplication(sys.argv)
GUI = MainWindow()
GUI.show()
while True:
d = GIFChanger()
t = QVBoxLayout()
text = input("?: ")
if "speak" in text:
d.changeToSpeech()
t.addWidget(d)
sys.exit(MyApp.exec_())