0

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
its_prs777
  • 19
  • 3
  • The only thing which I want to do is, I want the function in the Mainclass to run from the QThread class .. but it doesnt happening.? – its_prs777 Jan 11 '21 at 06:26
  • No, you cannot and should not modify the GUI in another thread, but you have to use the signals. – eyllanesc Jan 11 '21 at 06:31
  • @eyllanesc so, how can I use the signals in my code. Please help, I am new in pyqt5, I want to know how can I do this,it would resolve my many problems. What changes i have to make in my code? – its_prs777 Jan 11 '21 at 06:40
  • In both linked posts they implement that logic so I am not going to repeat it, I recommend you study it in addition to the fact that the use of signals is basic in Qt. That you are new to a certain technology is irrelevant so please do not use it as an excuse since it is only noise. – eyllanesc Jan 11 '21 at 06:44

0 Answers0