问题描述
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter.messageBox import showinfo
import tkinter as tk
import speech_recognition as sr
import nltk
# create the root window
root = tk.Tk()
root.title('Tkinter File Dialog')
root.resizable(False,False)
root.geometry('300x150')
def select_files():
filetypes = (
('audio files','*.wav'),)
file = fd.askopenfilenames(
title='Open file',initialdir='/',filetypes=filetypes)
showinfo(
title='Selected File',message=file
)
r = sr.Recognizer()
if file is not None:
tk.messageBox.showinfo("result","file added successfully")
with file as source:
audiof = r.record(source,duration=20)
resultf = r.recognize_google(audiof)
contentf = file.read()
with open("runtimefile.txt","w") as text_file:
text_file.write("%s" % resultf)
with open("runtimefile.txt","r") as file:
file_content = file.read()
runtokens = nltk.word_tokenize(file_content)
print("tokens for runtime file",runtokens)
text_file.close()
# open button
open_button = ttk.Button(
root,text='Open Files',command=select_files
)
open_button.pack(expand=True)
root.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Tani\Anaconda3\envs\pythonProject\lib\tkinter\__init__.py",line 1892,in __call__
return self.func(*args)
File "C:/Users/Tani/PycharmProjects/pythonProject/microphone.py",line 33,in
select_files
with file as source:
AttributeError: __enter__
我试图在 python 中在运行时将音频文件作为输入,然后通过将其转换为文本文件并标记化作为输入的文件内容来读取该文件,但系统给了我类似上面提到的错误
>请建议我解决这个问题
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)