Advertisement
DrAungWinHtut

ttstranslategyi.py

Apr 22nd, 2023
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. from gtts import gTTS
  2. import os
  3. from pydub import AudioSegment
  4. from pydub.playback import play
  5. from deep_translator import GoogleTranslator
  6. from googletrans import Translator, LANGUAGES
  7. import tkinter as tk
  8. from tkinter import ttk
  9.  
  10.  
  11. class TranslationApp(tk.Tk):
  12.     def __init__(self):
  13.         super().__init__()
  14.  
  15.         self.title("Translation Software")
  16.         self.geometry("400x300")
  17.  
  18.         self.translator = Translator()
  19.  
  20.         self.create_widgets()
  21.  
  22.     def create_widgets(self):
  23.         self.from_label = ttk.Label(self, text="From:")
  24.         self.from_label.grid(column=0, row=0, padx=10, pady=10)
  25.  
  26.         self.from_language = ttk.Combobox(self, values=list(LANGUAGES.values()), state="readonly")
  27.         self.from_language.set("english")
  28.         self.from_language.grid(column=1, row=0, padx=10, pady=10)
  29.  
  30.         self.to_label = ttk.Label(self, text="To:")
  31.         self.to_label.grid(column=2, row=0, padx=10, pady=10)
  32.  
  33.         self.to_language = ttk.Combobox(self, values=list(LANGUAGES.values()), state="readonly")
  34.         self.to_language.set("myanmar (burmese)")
  35.         self.to_language.grid(column=3, row=0, padx=10, pady=10)
  36.  
  37.         self.text_to_translate = tk.Text(self, wrap=tk.WORD, height=5)
  38.         self.text_to_translate.grid(column=0, row=1, columnspan=4, padx=10, pady=10)
  39.  
  40.         self.translate_button = ttk.Button(self, text="Translate", command=self.translate_text)
  41.         self.translate_button.grid(column=0, row=2, columnspan=4)
  42.  
  43.         self.translated_text = tk.Text(self, wrap=tk.WORD, height=5, state="disabled")
  44.         self.translated_text.grid(column=0, row=3, columnspan=4, padx=10, pady=10)
  45.  
  46.     def translate_text(self):
  47.         source_text = self.text_to_translate.get(1.0, tk.END).strip()
  48.         from_language = self.from_language.get()
  49.         to_language = self.to_language.get()
  50.  
  51.         if source_text and from_language and to_language:
  52.             from_language_code = None
  53.             to_language_code = None
  54.  
  55.             for code, language in LANGUAGES.items():
  56.                 if language == from_language:
  57.                     from_language_code = code
  58.                 if language == to_language:
  59.                     to_language_code = code
  60.                 if from_language_code and to_language_code:
  61.                     break
  62.  
  63.             if from_language_code and to_language_code:
  64.                 translation = self.translator.translate(source_text, src=from_language_code, dest=to_language_code)
  65.  
  66.                 self.translated_text.config(state="normal")
  67.                 self.translated_text.delete(1.0, tk.END)
  68.                 self.translated_text.insert(tk.END, translation.text)
  69.                 self.translated_text.config(state="disabled")
  70.             else:
  71.                 print("Error: Language not found.")
  72.  
  73.  
  74.  
  75.  
  76.  
  77. def trans(text,language):
  78.     translated = GoogleTranslator(target=language).translate(text)
  79.     return translated
  80.  
  81. def text_to_speech(text, language, output_file):
  82.     tts = gTTS(text=text, lang=language, slow=False)
  83.     tts.save(output_file)
  84.  
  85. def play_audio(file):
  86.     audio = AudioSegment.from_file(file)
  87.     play(audio)
  88.  
  89. # strating point
  90. if __name__ == "__main__":
  91.     app = TranslationApp()
  92.     app.mainloop()
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement