Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import *
- from tkinter import filedialog, messagebox # Added messagebox
- from tkinter.ttk import Combobox, Style
- import pyttsx3
- import os
- import pytesseract
- from PIL import Image, ImageTk
- root = Tk()
- root.title("Text to Speech")
- root.geometry("900x500+100+100")
- root.resizable(False, False)
- root.configure(bg="#1A3B59")
- # Functions
- engine = pyttsx3.init()
- def setvoice(gender):
- voices = engine.getProperty('voices')
- if (gender == 'David'):
- engine.setProperty('voice', voices[0].id)
- elif(gender=='Tony'):
- engine.setProperty('voice', voices[1].id)
- elif(gender=='Sam'):
- engine.setProperty('voice', voices[2].id)
- elif(gender=='Meera'):
- engine.setProperty('voice', voices[3].id)
- else:
- engine.setProperty('voice', voices[4].id)
- def speaknow():
- text = text_area.get(1.0, END).strip()
- if not text:
- messagebox.showwarning("No Text", "Please enter some text before speaking.") # Show warning message
- return
- gender = gender_combobox.get()
- speed = speed_combobox.get()
- if (speed == "Fast"):
- engine.setProperty('rate', 250)
- elif (speed == "Normal"):
- engine.setProperty('rate', 150)
- else:
- engine.setProperty('rate', 60)
- setvoice(gender)
- def run_pyttsx3(text):
- engine.say(text)
- engine.runAndWait()
- import threading
- threading.Thread(
- target=run_pyttsx3, args=(text,), daemon=True
- ).start()
- def download():
- text = text_area.get(1.0, END).strip()
- if not text:
- messagebox.showwarning("No Text", "Please enter some text before downloading.") # Show warning message
- return
- gender = gender_combobox.get()
- speed = speed_combobox.get()
- if (speed == "Fast"):
- engine.setProperty('rate', 250)
- elif (speed == "Normal"):
- engine.setProperty('rate', 150)
- else:
- engine.setProperty('rate', 60)
- setvoice(gender)
- file_path = filedialog.asksaveasfilename(defaultextension=file_extension, filetypes=file_types)
- if file_path:
- engine.save_to_file(text, file_path)
- # engine.runAndWait()
- def open_image():
- file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png *.jpg *.jpeg")])
- if file_path:
- img = Image.open(file_path)
- text = pytesseract.image_to_string(img)
- text_area.insert(END, text)
- # File types for saving
- file_types = [('MP3 Files', '*.mp3'), ('WAV Files', '*.wav'), ('OGG Files', '*.ogg')]
- file_extension = file_types[0][1][1:] # Default file extension is MP3
- # Icon
- image_icon = PhotoImage(file="speak.png")
- root.iconphoto(False, image_icon)
- # Top frame
- Top_frame = Frame(root, bg="#FFFFFF", width=900, height=100)
- Top_frame.place(x=0, y=0)
- Logo = PhotoImage(file="speaker_logo.png")
- Label(Top_frame, image=Logo, bg="#FFFFFF").place(x=10, y=5)
- Label(Top_frame, text="TEXT TO SPEECH", font="Arial 20 bold", bg="#FFFFFF", fg="#1A3B59").place(x=100, y=30)
- # Text area
- text_area = Text(root, font="Roboto 16", bg="#EFF4F4", relief=GROOVE, wrap=WORD, borderwidth=2, padx=10, pady=10)
- text_area.place(x=10, y=150, width=500, height=250)
- # Rounded corners for the text area
- radius = 20
- text_area.config(highlightbackground="#BFBFBF", highlightthickness=1, highlightcolor="#BFBFBF")
- text_area.bind("<Enter>", lambda e: text_area.config(highlightbackground="#1A3B59", highlightthickness=2))
- text_area.bind("<Leave>", lambda e: text_area.config(highlightbackground="#BFBFBF", highlightthickness=1))
- # Label for Voices
- Label(root, text="VOICE", font="Arial 14 bold", bg="#1A3B59", fg="#FFFFFF").place(x=580, y=160)
- # Label for speed
- Label(root, text="SPEED", font="Arial 14 bold", bg="#1A3B59", fg="#FFFFFF").place(x=760, y=160)
- # Custom style for dropdown boxes
- style = Style()
- style.theme_create("CustomStyle", settings={
- "TCombobox": {
- "configure": {
- "padding": 5,
- "background": "#F5F5F5",
- "foreground": "#333333",
- "fieldbackground": "#F5F5F5",
- "bordercolor": "#BFBFBF",
- "selectbackground": "#1A3B59",
- "selectforeground": "#FFFFFF",
- "arrowcolor": "#1A3B59",
- "borderwidth": 1, # Add border width
- "relief": "solid", # Add relief
- "border-radius": 5 # Add border radius
- }
- },
- "TMenubutton": {
- "configure": {"padding": 5}
- },
- "TButton": { # Style for buttons
- "configure": {
- "borderwidth": 1, # Add border width
- "relief": "solid", # Add relief
- "border-radius": 5 # Add border radius
- }
- }
- })
- style.theme_use("CustomStyle")
- # Gender combobox
- gender_combobox = Combobox(root, values=['David', 'Tony','Sam','Meera','Zara'], font="Arial 12", state="readonly", width=10)
- gender_combobox.place(x=550, y=200)
- gender_combobox.set('David')
- # Speed combobox
- speed_combobox = Combobox(root, values=['Fast', 'Normal', 'Slow'], font="Arial 12", state="readonly", width=10)
- speed_combobox.place(x=730, y=200)
- speed_combobox.set('Normal')
- # Image Button
- imageicon3 = PhotoImage(file="image.png")
- image_btn = Button(root, text="Add\nImage", compound=LEFT, image=imageicon3, width=120, font="arial 13 bold", command=open_image)
- image_btn.place(x=550, y=260) # Adjust the coordinates as needed
- # Speak Button
- imageicon = PhotoImage(file="speaking.png")
- btn = Button(root, text="Speak", compound=LEFT, image=imageicon, width=120, font="arial 14 bold", command=speaknow)
- btn.place(x=550, y=360) # Adjust the coordinates as needed
- # Save Button
- imageicon2 = PhotoImage(file="save-file.png")
- save = Button(root, text="Save", compound=LEFT, image=imageicon2, width=120, bg="#EFF4F4", font="arial 14 bold", command=download)
- save.place(x=730, y=360) # Adjust the coordinates as needed
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment