Created
June 27, 2025 10:23
-
-
Save Looons404/ac34609a36ffd31f6f46850a9b8d274c to your computer and use it in GitHub Desktop.
Demo of pysstx3 Text-To-Speech with thkinter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tkinter as tk | |
| from tkinter import ttk | |
| import pyttsx3 | |
| engine = pyttsx3.init() | |
| current_rate = engine.getProperty('rate') | |
| current_volume = engine.getProperty('volume') | |
| voices = engine.getProperty('voices') | |
| current_voice_index = 0 # default | |
| def apply_settings(rate, volume, voice_index): | |
| engine.setProperty('rate', rate) | |
| engine.setProperty('volume', volume) | |
| engine.setProperty('voice', voices[voice_index].id) | |
| def main_window(): | |
| def speak(): | |
| text = entry.get() | |
| if text.strip(): | |
| engine.say(text) | |
| engine.runAndWait() | |
| def open_settings(): | |
| root.destroy() | |
| settings_window() | |
| root = tk.Tk() | |
| root.title("Text-to-Speech") | |
| root.geometry("400x180") | |
| root.resizable(False, False) | |
| label = tk.Label(root, text="Enter your text:", font=("Arial", 12)) | |
| label.pack(pady=10) | |
| entry = tk.Entry(root, font=("Arial", 12), width=40) | |
| entry.pack() | |
| button_frame = tk.Frame(root) | |
| button_frame.pack(pady=10) | |
| speak_btn = tk.Button(button_frame, text="Speak", font=("Arial", 12), command=speak) | |
| speak_btn.grid(row=0, column=0, padx=10) | |
| settings_btn = tk.Button(button_frame, text="Settings", font=("Arial", 12), command=open_settings) | |
| settings_btn.grid(row=0, column=1, padx=10) | |
| root.mainloop() | |
| def settings_window(): | |
| def save_and_back(): | |
| selected_rate = int(rate_slider.get()) | |
| selected_volume = float(volume_slider.get()) | |
| selected_voice_index = voice_combo.current() | |
| apply_settings(selected_rate, selected_volume, selected_voice_index) | |
| settings.destroy() | |
| main_window() | |
| settings = tk.Tk() | |
| settings.title("Settings") | |
| settings.geometry("400x300") | |
| settings.resizable(False, False) | |
| tk.Label(settings, text="Speech rate:", font=("Arial", 11)).pack(pady=(10, 0)) | |
| rate_slider = tk.Scale(settings, from_=50, to=300, orient='horizontal') | |
| rate_slider.set(current_rate) | |
| rate_slider.pack() | |
| tk.Label(settings, text="Volume:", font=("Arial", 11)).pack(pady=(10, 0)) | |
| volume_slider = tk.Scale(settings, from_=0, to=1, resolution=0.1, orient='horizontal') | |
| volume_slider.set(current_volume) | |
| volume_slider.pack() | |
| tk.Label(settings, text="Voice:", font=("Arial", 11)).pack(pady=(10, 0)) | |
| voice_combo = ttk.Combobox(settings, values=[v.name for v in voices], state="readonly") | |
| voice_combo.current(current_voice_index) | |
| voice_combo.pack() | |
| tk.Button(settings, text="Save and return", font=("Arial", 12), command=save_and_back).pack(pady=20) | |
| settings.mainloop() | |
| apply_settings(current_rate, current_volume, current_voice_index) | |
| main_window() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment