我为说话字典写了这段代码。直到我编写代码的地方都可以正常运行,但是当我按下麦克风按钮上的“文本转语音”功能时。在该声音说出该输入字段上写的字后,GUI窗口会自行关闭。如在GUI窗口中一样,文本转语音功能后仍保持打开状态。有什么问题吗?
p.s。我有Macbook,所以请按:p
from tkinter import *
from tkinter import messagebox
import pyttsx3
engine=pyttsx3.init()
voices=engine.getProperty('voices')
engine.setProperty('voices',voices[1].id)
#*************************************FUNCTIONALITY*************************************
#function to quit
def iexit():
result=messagebox.askyesno('Confirm','Are you sure you want to quit ?')
if result==True:
root.destroy()
else:
pass
#function for voice
#
def word_audio():
engine.say(entry_field.get())
engine.runAndWait()
def meaning_audio():
engine.say(text_area.get(1.0,END))
engine.runAndWait()
def clear():
text_area.delete(1.0,END)
entry_field.delete(0,END)
root=Tk()
root.title('Talking Dictionary')
root.geometry('1000x626+300+200') #setting width and height 300 is for x and 200 for y
root.resizable(0,0)
bg_img=PhotoImage(file='bg.png')
bg_label=Label(root,image=bg_img)
bg_label.place(x=0,y=0)
enter_word_label=Label(root,text='Enter Word',font=('castellar',29,'bold'),bg='whitesmoke',fg='red3')
enter_word_label.place(x=530,y=20)
#for entry field
entry_field=Entry(root,font=('arial',23,'bold'),bd=8,relief=GROOVE,justify=CENTER)
entry_field.place(x=510,y=80)
entry_field.focus_set()
#for search button
search_img=PhotoImage(file='search.png')
search_button=Button(root,image=search_img,bd=0 ,activebackground='whitesmoke',cursor='hand',bg='whitesmoke')
search_button.place(x=570,y=150)
#for mic image
mic_img=PhotoImage(file='microphone.png')
mic_button=Button(root,image=mic_img, bd=0 ,activebackground='whitesmoke',cursor='hand',background='white smoke',command=word_audio)
mic_button.place(x=670,y=150)
meaning_label=Label(root,text='Meaning',font=('castellar',35,'bold'),bg='white smoke',fg='red3')
meaning_label.place(x=550,y=250)
text_area=Text(root,font=('arial',18,'bold'),width=34,height=8,bd=8,relief=GROOVE)
text_area.place(x=460,y=300)
audio_img=PhotoImage(file='microphone.png')
audio_button=Button(root,image=audio_img,bd=0,cursor='hand',bg='whitesmoke',command=meaning_audio)
audio_button.place(x=510,y=510)
clear_img=PhotoImage(file='clear.png')
clear_button=Button(root,image=clear_img,bd=0,cursor='hand',command=clear)
clear_button.place(x=610,y=510)
exit_img=PhotoImage(file='exit.png')
exit_button=Button(root,image=exit_img,bd=0,cursor='hand',bg='whitesmoke',command=iexit)
exit_button.place(x=720,y=510)
root.mainloop()