This is a code for a virtual assistant this code terminates after the user speaks once. I need this code to iterate till the user says exit. I tried using 'while True' after 'if name == "main": wishme()' it runs, but in that case it does not show the tkinter window. Can you help me please? Here is my code
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
from tkinter import *
root = Tk()
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
print(voices[0].id)
engine.setProperty('voice', voices[0].id)
def speak(audio):
e1 = Text(root)
e1.grid(row=0, column=0)
engine.say(audio)
engine.runAndWait()
t = Text(root)
t.grid(row = 0, column = 0,
columnspan = 2, rowspan = 2, padx = 5, pady = 5)
t.insert(END, 'Genos says: '+ audio +'\n')
def wishme():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak("Good Morning sir")
elif hour>=12 and hour<18:
speak("Good Afternoon sir")
else:
speak("Good Evening sir")
speak("I am Genos. How can I Serve you?")
return
def takecommand():
# it takes mic input from the user and return string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing..")
query = r.recognize_google(audio, language='en-in')
print(f"user Said :{query}\n")
except Exception as e:
print(e)
print("Say that again please")
return "None"
t1 = Text(root)
t1.grid(row = 0, column = 2,
columnspan = 2, rowspan = 2, padx = 5, pady = 5)
t1.insert(END, 'user says: '+query+'\n')
return query
def sendEmail(to, content):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login('your email@gmail.com','password')
server.sendmail('your email@gmail.com',to,content)
server.close()
return
if __name__ == "__main__":
wishme()
#while True:
if 1:
query = takecommand().lower()
#Logic for executing task based query
if 'wikipedia' in query:
speak('searching Wikipedia....')
query = query.replace("wikpedia", "")
results = wikipedia.summary(query, sentences=5)
speak("According to wikipedia")
print(results)
speak(results)
elif 'open youtube' in query:
webbrowser.open("youtube.com")
elif 'open google' in query:
webbrowser.open("google.com")
elif 'open stackoverflow' in query:
webbrowser.open("stackoverflow.com")
elif 'play music' in query:
music_dir = 'D:\\SAHIL\\$ONGS_MJ'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir the time is {strTime}")
elif 'open code' in query:
codepath = "C:\\Users\\Sahil\\AppData\\Local\\Programs\\Microsoft VS Code Insiders\\Code
- Insiders.exe"
os.startfile(codepath)
elif 'email to sahil' in query:
try:
speak("What should i say ?")
content = takecommand()
to = "your email11@gmail.com"
sendEmail(to, content)
speak("Email has been sent")
except Exception as e:
print(e)
speak("Could nott send the email ")
elif 'open mailbox' in query:
webbrowser.open("gmail.com");
elif 'exit' in query:
exit()
root.mainloop()