为什么PyQT5程序没有响应?

问题描述

我正在尝试使用PyQT5制作应用。我正在IDLE-IDLE(Python 3.8 32-bit)中编辑代码,并在那里进行编译。因此,当程序启动时,我单击按钮,程序冻结,并说“不响应”-“ Python没有响应,请关闭Python?”但是它有时说,例如当我说“你好”时,它回答我“你好,你好吗?”。 这是我的代码

import sys
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtWidgets import QApplication
import speech_recognition as sr
import pyttsx3

engine = pyttsx3.init()

class Ui_MainWindow(object):
    def setupUi(self,MainWindow):
        MainWindow.setobjectName("MainWindow")
        MainWindow.resize(888,555)
        MainWindow.setMinimumSize(QtCore.QSize(888,555))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setobjectName("centralwidget")
# Button
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(425,375,75,75))
        self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.pushButton.setobjectName("pushButton")
        self.pushButton.clicked.connect(listen)
# Output
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(240,120,444,222))
        self.label.setMinimumSize(QtCore.QSize(444,222))
        self.label.setMaximumSize(QtCore.QSize(444,222))
        self.label.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.label.setAutoFillBackground(False)
        self.label.setText("")
        self.label.setobjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self,MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setwindowTitle(_translate("MainWindow","Prog"))
        self.pushButton.setText(_translate("MainWindow","?️"))

def listen():
    while True:
        ui.label.setText("I'm listening...")
        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            said = ''

            try:
                said = r.recognize_google(audio,language="en-EN")
                print(said)
            except sr.UnkNownValueError:
                engine.say("I didn't catch that")
                engine.runAndWait()
            except sr.RequestError:
                engine.say("Oops,looks like there is no internet connection")
                engine.runAndWait()

            if 'hello' in said:
                engine.say("Hi,how are you?")
                engine.runAndWait()



if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

请帮助我

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)