将音频转换为文本并将其插入到 tkinter 的窗口中会产生 AttributeError: 'dict' object has no attribute 'Text'

问题描述 投票:0回答:1
  • 您好,我是越南人。我正在尝试使用下面的代码将音频转换为文本并插入到 tkinter 窗口中,但出现以下错误: 在此输入图片描述

  • 您能否分享您的想法来解决这个问题,或者建议可以执行转录的替代库或人工智能?

  • 代码:

from tkinter import *
from PIL import Image,ImageTk
from googletrans import Translator
import speech_recognition
import re

#create a window
root=Tk()
root.title('Google Translate')
root.geometry("500x630")
root.iconbitmap(r'images.ico')

#Insert images for buttons
img_import = (Image.open(r'xoa.png'))
resize = img_import.resize((25,25), Image.LANCZOS)
img = ImageTk.PhotoImage(resize)

img_import1 = (Image.open(r'mic.png'))
resize1 = img_import1.resize((25,25), Image.LANCZOS)
img1 = ImageTk.PhotoImage(resize1)

img_import2 = (Image.open(r'loa.png'))
resize2 = img_import2.resize((25,25), Image.LANCZOS)
img2 = ImageTk.PhotoImage(resize2)

img_import3 = (Image.open(r'h.png'))
resize3 = img_import3.resize((25,25), Image.LANCZOS)
img3 = ImageTk.PhotoImage(resize3)

#Used to do part of the function of the buttons
c = "Việt"
b = "Anh"
d = c

n = Label(root, text=c, font=("Arial",20))
n.place(x=190,y=5)

m = Label(root, text=b, font=("Arial",20))
m.place(x=290,y=5)

k = Label(root, text="by Nguyen Hoang Phuc", font=("Arial",7), fg="#0099FF")
k.place(x=390,y=615)

p = 'vi'
q = 'en'
o = p

box=Text(root,width=28,height=8,font=("ROBOTO",16))
box.pack(pady=40)

button_frame=Frame(root).pack(side=BOTTOM)

#Buttons
def clear():
    box.delete(1.0,END)
    box1.delete(1.0,END)

def translate():
    try:
        box1.delete(1.0,END)
        INPUT=box.get(1.0,END)
        t=Translator()
        a=t.translate(INPUT,src=p,dest=q).text
        box1.insert(END,a)
    except:
        a=""
        box1.insert(END,a)
def mic():
    micro = speech_recognition.Recognizer()
    with speech_recognition.Microphone() as mic:
        audio = micro.listen(mic)
        ghi = micro.recognize_google(audio, language = 'vi', show_all = True)
        ghi1 = ghi.Text
    def translate():
        try:
            box1.delete(1.0,END)
            box.insert(ghi1,END)
            t=Translator()
            a=t.translate(ghi1,src=p,dest=q).text
            box1.insert(END,a)
        except:
            a=""
            box1.insert(END,a)

def loa():
    pass
def loa2():
    pass
def convert():
    global translate_button
    del(translate_button)   
    global p
    global q
    global o
    global b
    global c
    global d
    o = p
    p = q
    q = o
    d = c
    c = b
    b = d
    n = Label(root, text=c, font=("Arial",20))
    n.place(x=190,y=5)
    m = Label(root, text=b, font=("Arial",20))
    m.place(x=290,y=5)
    def translate():
        try:
            box1.delete(1.0,END)
            INPUT = box.get(1.0,END)
            t=Translator()
            a=t.translate(INPUT,src=p,dest=q).text
            box1.insert(END,a)
        except:
            a=""
            box1.insert(END,a)
    translate_button=Button(button_frame, text = "Dịch", font= 
('Arial',10),bg='#FFFFFF',fg="#000000",command=translate)
    translate_button.place(x = 235,y = 260)

#utton location
translate_button=Button(button_frame, text = "Dịch", font= [tag:tag-name]('Arial',10),bg='#FFFFFF',fg="#000000",command=translate)
translate_button.place(x = 235,y = 260)

clear_button=Button(button_frame, text = '', font = ('Time New Roman',11), image = img,command=clear)
clear_button.place(x = 420,y = 40)

mic_button=Button(button_frame, text = '', font = ('Time New Roman',11), image = img1,command=mic)
mic_button.place(x = 80,y = 235)

loa_button=Button(button_frame, text = '', font = ('Time New Roman',11), image = img2,command=loa)
loa_button.place(x = 110,y = 235)

loa2_button=Button(button_frame, text = '', font = ('Time New Roman',11), image = img2,command=loa2)
loa2_button.place(x = 80,y = 510)

loa2_button=Button(button_frame, text = '', font = ('Time New Roman',11), image = img2,command=loa2)
loa2_button.place(x = 80,y = 510)

convert_button=Button(button_frame, text = '', font = ('Time New Roman',11), image = img3,command=convert)
convert_button.place(x = 250,y = 5)

box1=Text(root,width=28,height=8,font=("ROBOTO",16))
box1.pack(pady=40)

root.mainloop()

希望你能帮助我

谢谢

python tkinter speech-recognition pyttsx3 translators
1个回答
0
投票

代码中的问题出在 mic() 函数内,特别是在 ghi1 = ghi.Text 行处。 recognize_google 方法返回字典,并且您正在尝试访问字典中不存在的文本属性。相反,您需要直接访问字典的相应部分。

© www.soinside.com 2019 - 2024. All rights reserved.