You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.1 KiB

import tkinter as tk
import pyttsx3
import speech_recognition as sr
from tkinter import filedialog
import threading
engine = pyttsx3.init()
def center_window(window, width=200, height=150):
"""使窗口居中显示"""
window.update_idletasks() # 更新窗口,确保尺寸是最新的
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
if width is None:
width = window.winfo_width()
if height is None:
height = window.winfo_height()
x_cordinate = int((screen_width / 2) - (width / 2))
y_cordinate = int((screen_height / 2) - (height / 2))
window.geometry(f"{width}x{height}+{x_cordinate}+{y_cordinate}")
def create_text_to_speech_window(parent):
global text_entry, result_label
window = tk.Toplevel(parent)
window.title("文本转语音")
speed_slider = tk.Scale(window, from_=50, to=200, orient=tk.HORIZONTAL,command=lambda value: set_speed(value, engine))
speed_slider.set(120) # 默认语速
speed_slider.pack()
label = tk.Label(window, text="请输入文字:")
label.pack(pady=10)
# 创建一个文本框,用于用户输入
text_entry = tk.Text(window, height=20)
text_entry.pack()
convert_button = tk.Button(window, text="转换语音", command=text_to_speech)
convert_button.pack(side=tk.LEFT, padx=250, pady=10)
result_label = tk.Label(window, text="", fg="green")
result_label.pack(pady=5)
def text_to_speech():
engine = pyttsx3.init()
text = text_entry.get("1.0", "end-1c") # 获取文本框中的文本
engine.say(text)
engine.runAndWait()
def set_speed(value, engine):
"""根据滑块的值设置语音合成的语速"""
engine.setProperty('rate', int(value))
def create_audio_recognition_window(window):
window = tk.Toplevel(window)
window.title("音频文件识别")
# 正确的写法
recognize_button = tk.Button(window, text="音频识别",command=lambda win=window: audio_recognition_window())
recognize_button.pack(side=tk.RIGHT, padx=70, pady=50)
# 创建结果标签
result_label = tk.Label(window, text="")
result_label.pack()
center_window(window)
def audio_recognition_window():
global text_entry, result_label
window = tk.Toplevel()
window.title("音频")
select_button = tk.Button(window, text="选择音频文件",command=lambda win=window: recognize_audio_from_file())
select_button.pack(pady=50)
result_label = tk.Label(window, text="", fg="green")
result_label.pack(pady=5)
center_window(window)
# 运行Tkinter事件循环
window.mainloop()
def recognize_audio_from_file():
window = tk.Toplevel()
window.title("文本")
# 打开文件对话框选择音频文件
def worker():
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav")])
if file_path:
r = sr.Recognizer()
with sr.AudioFile(file_path) as source:
audio_data = r.record(source)
text = r.recognize_sphinx(audio_data,language='zh-CN')
window.update_idletasks() # 更新窗口以避免同步问题
text_entry.delete(1.0, tk.END) # 清空文本框
text_entry.insert(tk.END, f"{text}")
text_entry = tk.Text(window, height=10, width=50)
text_entry.pack(pady=10)
thread = threading.Thread(target=worker)
thread.start()
def main():
window = tk.Tk()
window.title("选择界面")
window.geometry("400x300")
window.resizable(True, True)
# 创建并配置跳转到文本转语音窗口的按钮
text_to_speech_button = tk.Button(window, text="文本转语音", command=lambda: create_text_to_speech_window(window))
text_to_speech_button.pack(padx=10,pady=10)
# 创建并配置跳转到音频识别窗口的按钮
audio_recognition_button = tk.Button(window, text="音频文件识别", command=lambda: create_audio_recognition_window(window))
audio_recognition_button.pack(padx=10,pady=10)
center_window(window)
window.mainloop()
if __name__ == "__main__":
main()