I'm working on a voice assistant, and I can't transfer the value to the chat. Because self.textEdit.setText () does not work in the child class, but in the parent class it works.

I tried to create a global variable, and use super().

self.show () in my program does not work, because when the user sets a command, self.show will create a new window.

My Code

class Virtual_Assistant(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)

        self.pushButton.clicked.connect(self.commands)

        self._recognizer = sr.Recognizer()
        self._microphone = sr.Microphone()

        self.textEdit.setPlainText('Virtual Assistant')

    def speech_recognition(self):
        """Recognize speech"""

        with self._microphone as source:
            self.speech_synthesizer('Im listening')
            print('Speak')
            audio = self._recognizer.record(source, duration = 3)  # РЕШИТЬ ПРОБЛЕМУ С ОСКОРОСТЬЮ РАБОТЫ self.duration
            try:
                text = self._recognizer.recognize_google(audio, language="ru_RU")
                text = text.lower()
                print(text.lower())
                return text
            except sr.UnknownValueError:
                self.speech_synthesizer('You did not say anything')


    def speech_synthesizer(self, text):
        """Synthesize speech"""
        engine = pyttsx3.init()
        ru_voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_RU-RU_IRINA_11.0"
        engine.setProperty('rate', 200)
        engine.setProperty('volume', 1)
        engine.setProperty('voice', ru_voice_id)
        text = engine.say(text)
        engine.runAndWait()
        return text

    def commands(self):
        """commands"""
        phrase = self.speech_recognition()


        if phrase.find('weather for today') != -1:
            Weather().weather('weather for today')


class Weather(Virtual_Assistant,QtWidgets.QMainWindow, Ui_MainWindow):
    """Weather forecast"""

    def weather(self, choice):

        BASE_URL = 'https://sinoptik.ua/погода-винница/10-дней'

        r = requests.get(BASE_URL)

        soap = BeautifulSoup(r.text, 'html.parser')
        temperature = soap.select('div.temperature')
        date = soap.select('p.date')
        weather = soap.select('div.wDescription.clearfix')
        days = soap.select('a.day-link')

        days_information = []
        for day in days:
            days_information.append(day.get_text())

        weather_information = []
        for i in weather:
            weather = i.get_text().strip()
            weather_information.append(weather)

        date_information = []
        for value in date:
            text = value.get_text()
            date_information.append(text)

        temperature_information = []
        for msg in temperature:
            txt = msg.get_text().replace('min.', 'max').replace('min.', 'max')
            temperature_information.append(txt)

        if choice == 'weather for today':

            ##### My problem is, the weather is not displayed in the current window ####
            self.textEdit.setText('weather for today ' + temperature_information[0] + weather_information[0]) # PROBLEM


            self.speech_synthesizer('weather for today ' + temperature_information[0] + weather_information[0])


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Virtual_Assistant()
    window.show()
    sys.exit(app.exec())

I want the weather to be transmitted to the chat from the child class. https://imgur.com/W4XxabO

0 Answers