首页 > 解决方案 > gui tkinter python3 不工作总是停止

问题描述

我的tkinter gui代码是-

此代码显示我的 gui没有响应

我正在制作语音助手此代码我从另一个网站引用并尝试在我的 spyder 或 jupyter 中运行但无法运行它,因为它显示没有响应它根本没有将语音作为输入

我读了一些堆栈,但我不确定它是否有帮助,所以想要一些关于代码有什么问题的帮助

我读了一些帖子多线程会工作我应该试试吗?

from tkinter import*
import pyttsx3
import speech_recognition as sr
import pyaudio
import random
import time
from tkinter import messagebox
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class Window(Frame):
    #defining our main window
    def __init__(self,master):
        self.master=master
        master.title("DREAM")
        A=Label(master,text="try saying *what can you do*")
        A.pack(side="top")
        Button(master,text="listen",width=100,relief="groove",command=self.Processo_r).pack(side="bottom")
    def Processo_r(self):
        speech=str(self.speech_recog())

        if speech=="What can you do":
            self.functions()  
    def speak(self,output):
        #initiating the speech engine
        engine = pyttsx3.init()
        #speaking the desired output 
        engine.say(output)
        engine.runAndWait()
    def functions(self):
        self.speak("here is a list of what i can do")
        messagebox.showinfo("DREAM functions", "1.Try saying 'Hi','Hello'" +
                            "\n2.Try asking 'What day is this?'" +
                            "\n3.Try asking 'What month is it?'" +
                            "\n4.Try asking 'What time is it?'" +
                            "\n5.You search in google by saying...'Search (or) Google <anything>'" +
                            "\n6.Play youtube by saying'YouTube... <video_name>'" +
                            "\n7.Search in Wikipedia by saying...'wikipedia...<anything>'" +
                            "\n8.To close say 'Bye' or 'Sleep' or 'See you later'")

    def speech_recog(self):
        #recognizer class
        r=sr.Recognizer()
        #Specifing the microphone to be activated
        mic = sr.Microphone(device_index=1)

        #listening to the user 
        with mic as s:
            audio = r.listen(s, timeout=5)
            r.adjust_for_ambient_noise(s)

        #Converting the audio to text  
        try:
            speech = r.recognize_google(audio)
            return speech

        except sr.UnknownValueError:
            #calling the text to speech function
            self.speak("please try again,couldnt identify")

        
        except sr.WaitTimeoutError as e:
            self.speak("please try again") 
    
    
root=Tk()
#instance of the class  
app=Window(root)
root.geometry("300x50")
#Runs the application until we close
root.mainloop()

标签: pythonpython-3.xuser-interfacetkinter

解决方案


推荐阅读