I have created a basic GUI window in Python using Pyqt5, having one Label named: label_1 and one button named: btn_1 I want some codes to run in background, the code is having while loop so, it would freeze the GUI. So, here I used QThreading of Pyqt5 to do so.
But, I have a problem I want something to be triggered in Mainwindow class (GUI) from the Threading Class. How can I do that...
Here's the code,
import speech_recognition as sr
from PySide2 import QtCore, QtWidgets
from PySide2.QtCore import Qt, QThread
from PySide2.QtWidgets import QApplication, QMainWindow
from ui_AI import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btn_1.clicked.connect(self.start_listen)
def start_listen(self):
StartMain_GUI.start()
def function_i_want_to_do(self):
sefl.ui.label_1.setText('Done')
class MainThread(QThread):
def __init__(self):
super(MainThread, self).__init__()
def run(self):
self.takecmd()
def takecmd(self):
r = sr.Recognizer()
with sr.Microphone() as source:
print('listening...')
r.pause_threshold = 1
audio = r.listen(source)
main = MainWindow()
main.function_i_want_to_do()
try:
print("Recognizing...")
recog_txt = r.recognize_google(audio,language=en-in)
return recog_txt
main = MainWindow()
main.function_i_want_to_do()
except Exception:
print("Unable to Recognize your voice.")
return ""
StartMain_GUI = MainThread()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())