import pyttsx3
import tkinter as tk
from tkinter import ttk
class TextToSpeech:
def __init__(self, master):
self.master = master
self.master.title("Text-to-Speech")
self.master.geometry("400x300")
self.engine = pyttsx3.init()
self.create_widgets()
def create_widgets(self):
# Text Input
self.text_label = ttk.Label(self.master, text="Enter Text:")
self.text_label.pack(pady=5)
self.text_entry = ttk.Entry(self.master, width=50)
self.text_entry.pack(pady=5)
# Voice Selection
self.voice_label = ttk.Label(self.master, text="Select Voice:")
self.voice_label.pack(pady=5)
self.voice_var = tk.StringVar()
self.voice_combo = ttk.Combobox(self.master, textvariable=self.voice_var)
self.voice_combo['values'] = [voice.id for voice in self.engine.getProperty('voices')]
self.voice_combo.pack(pady=5)
self.voice_combo.set(self.voice_combo['values'][0])
# Speed Control
self.speed_label = ttk.Label(self.master, text="Speech Speed:")
self.speed_label.pack(pady=5)
self.speed_scale = ttk.Scale(self.master, from_=100, to=400, orient='horizontal') # Set range for speech rate
self.speed_scale.set(200) # Default speed
self.speed_scale.pack(pady=5)
# Speak Button
self.speak_button = ttk.Button(self.master, text="Speak", command=self.speak_text)
self.speak_button.pack(pady=20)
def speak_text(self):
text = self.text_entry.get()
voice = self.voice_var.get()
speed = self.speed_scale.get()
self.engine.setProperty('voice', voice)
self.engine.setProperty('rate', speed)
self.engine.say(text)
self.engine.runAndWait()
if __name__ == "__main__":
root = tk.Tk()
app = TextToSpeech(root)
root.mainloop()
我已经安装了所有要求,但仍然无法运行。我正在尝试在我的 MacBook 上做一个小文本到语音项目,这是我在学校计算机上学习了一些代码后最近购买的,我想做这个项目来帮助我进行长篇阅读,因为有时我会迷失在文字中我期望代码运行良好,没有任何问题,但我总是在输出中得到这个。 [运行] python -u“/User/User/Deskstop/Projects/TTS.py” /bin/sh:python:找不到命令
您描述的问题意味着您正在命令行中执行您的文件。有以下几种可能:
python3
代替 python
。这可能会解决问题,特别是如果您安装了 python 2。