我有个问题要问你们天才们。 我正在用python做语音助手。当我将代码转换为python可执行文件并运行时。我的代码在命令提示符下运行,但是我希望它在一个简单的小GUI窗口中运行。 我不知道要制作GUI。
请帮助我为我的python语音助手创建GUI。
这是我的代码:
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import sys
from google import auth
from tkinter import *
Faizan_root = Tk()
Faizan_root.geometry("300x400")
Faizan_root.title("JARVIS AI by Faizan Jallani")
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak("Good Morning!")
elif hour>=12 and hour<18:
speak("Good Afternoon!")
else:
speak("Good Evening!")
speak("I am JARVIS. How may I help you Sir?")
def takeCommand():
r = sr.Recognizer()
with sr.Microphone () as source:
print("Listening...")
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=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)
speak("Say that again please...")
return"None"
return query
if __name__ == '__main__':
wishMe()
while True:
query = takeCommand().lower()
# Logic for executing tasks based on query.
if 'wikipedia' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2 )
speak("According to Wikipedia")
print(results)
speak(results)
elif 'open youtube' in query:
webbrowser.open("https://www.youtube.com/", new=2)
speak("Enjoy")
elif 'open google' in query:
webbrowser.open("https://www.google.com/", new=2)
elif 'open stack overflow' in query:
webbrowser.open("https://stackoverflow.com/", new=2)
elif 'play music' in query:
music_dir = 'F:\\songs\\Favorite'
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\\Faizan\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
os.startfile(codePath)
elif 'open facebook' in query:
webbrowser.open("https://www.facebook.com/", new=2)
elif 'instagram' in query:
webbrowser.open("https://www.instagram.com/faizangujjar01/", new=2)
speak("Have a look sir")
elif 'are you there' in query:
stMsgs = ['At you service, Sir']
speak(stMsgs)
elif 'what is' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak("According to Wikipedia")
print(results)
speak(results)
elif 'search for' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak("According to Wikipedia")
print(results)
speak(results)
elif 'shutdown the PC' in query:
choice = input("Please confirm to shutdown the pc (y or n)")
if choice == 'n':
exit()
else:
os.system("shutdown /s /t 1")
elif 'exit' in query:
sys.exit(speak("Ok sir, Take Care."))
Faizan_root.mainloop()
答案 0 :(得分:0)
通过GUI,我想您想要的不仅仅是控制台应用程序。您将不得不设计它。 例如。您需要取消菜单(具有“重新启动”,“关于”,“退出”之类的选项),或者只是某个机器人图像(具有动画效果的图像)。 Python随附TkInter打包,但是还有很多。
答案 1 :(得分:0)
我遇到了类似的问题,因此在阅读了一些博客后,我能够使用 tkinter 添加一个简单的用户界面。
这是我的代码:-
import tkinter as tk
import Assistant
from tkinter import*
from tkinter import messagebox
class Window(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
master.title("Anchit AI")
A = Label(master, text="Here to help you!")
A.pack(side="top")
Button(master, text="Tap to start!", width=100,relief="groove",command=self.Processo_r).pack(side="bottom")
def Processo_r(self):
Assistant.MainClass()
root = Tk()
#instance of the class
app = Window(root)
root.geometry("700x100")
#Runs the application until we close
root.mainloop()
Assistant.MainClass() 是其他 python 文件 Assistant.py 中的一个函数,它具有我的助手的主要功能逻辑
你可以去 https://github.com/Anirudh-thakur/Personal-Assistant 整个项目