【发布时间】:2021-07-26 23:52:39
【问题描述】:
我正在尝试在 Python 中使用 tkinter 制作语音助手应用程序,但我的代码有两个问题。
我的代码中的一个 sn-p:
def listen_to_me():
msg2 = Message(root, text="Listening...", bg='yellow', font=('times', 14, 'italic'))
msg2.pack
msg2.place(x=200, y=220)
with sr.Microphone() as source:
audio = r.listen(source)
global query
query = r.recognize_google(audio, language='en-IN', show_all=True)
if query:
try:
(f"[Me]: {query}")
except:
engine.say("Sorry didn't quite catch that. Please repeat.")
return query
def reply():
while True:
global query
# Logic for executing tasks based on query
if 'wikipedia' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
query = query.replace("search", "")
query = query.replace("for", "")
results = wikipedia.summary(query, sentences=1)
speak(f'According to Wikipedia, {results}')
elif 'open youtube' in query:
speak('Opening Youtube')
webbrowser.open("https://youtube.com")
elif 'open stack overflow' in query:
speak('Opening StackOverflow')
webbrowser.open("https://stackoverflow.com")
elif 'what' in query and 'time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir, The time is {strTime}")
elif 'how are you' or 'what\'s up' in query:
speak('I am doing jolly good, sir.')
问题 1: 我得到了输出,但它似乎陷入了循环:
[MyAssistant]: Good evening! I am Jarvis. How may I help you today?
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
问题 2: 我想将我的查询转换为小写。我尝试了以下方法:
query = query.lower()
错误:
AttributeError: 'list' object has no attribute 'lower'
提前致谢!
【问题讨论】:
-
对于第一个问题,只需删除
while True,对于第二个问题,它应该作为一个不同的问题提出。 -
这能回答你的问题吗? While loop not working while using Tkinter
标签: python tkinter speech-recognition