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.

45 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import tkinter as tk
from tkinter import ttk
import pyttsx3
def text_to_speech():
engine = pyttsx3.init()
text = text_entry.get("1.0", "end-1c") # 获取文本框中的文本
engine.say(text)
speed = speed_slider.get()
engine.setProperty('rate', speed)
engine.runAndWait()
def main():
global text_entry
# 创建主窗口
app = tk.Tk()
app.title("文本转语音")
speed_slider = ttk.Scale(app, from_=50, to=200, orient=tk.HORIZONTAL)
speed_slider.set(120) # 默认语速
speed_slider.pack()
# 创建一个标签,用于提示输入文本
label = tk.Label(app, text="请输入文字:")
label.pack(pady=10)
# 创建一个文本框,用于用户输入
text_entry = tk.Text(app, height=10)
text_entry.pack()
# 创建一个按钮点击时调用text_to_speech函数
convert_button = tk.Button(app, text="识别", command=text_to_speech)
convert_button.pack(pady=10)
# 运行主循环
app.mainloop()
if __name__ == "__main__":
main()