Skip to content

Commit 8f89ebe

Browse files
committed
add gui language translator code'
1 parent d62185c commit 8f89ebe

File tree

10 files changed

+141
-0
lines changed

10 files changed

+141
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
275275
- [How to Create a GUI Hangman Game using PyGame in Python](https://www.thepythoncode.com/article/hangman-gui-game-with-pygame-in-python). ([code](gui-programming/hangman-game-gui))
276276
- [How to Make a Tetris Game using PyGame in Python](https://www.thepythoncode.com/article/create-a-tetris-game-with-pygame-in-python). ([code](gui-programming/tetris-game))
277277
- [How to Build a Tic Tac Toe Game in Python](https://www.thepythoncode.com/article/make-a-tic-tac-toe-game-pygame-in-python). ([code](gui-programming/tictactoe-game))
278+
- [How to Build a GUI Language Translator App in Python](https://www.thepythoncode.com/article/build-a-gui-language-translator-tkinter-python). ([code](gui-programming/gui-language-translator))
278279

279280
For any feedback, please consider pulling requests.

Diff for: gui-programming/gui-language-translator/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Build a GUI Language Translator App in Python](https://www.thepythoncode.com/article/build-a-gui-language-translator-tkinter-python)

Diff for: gui-programming/gui-language-translator/arrows.png

4 KB
Loading

Diff for: gui-programming/gui-language-translator/copy.png

357 Bytes
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import ttkbootstrap as ttk
2+
from ttkbootstrap.scrolled import ScrolledText
3+
from ttkbootstrap.toast import ToastNotification
4+
from tkinter.messagebox import showerror
5+
import googletrans
6+
from googletrans import Translator
7+
import pyttsx3
8+
import pyperclip
9+
10+
translator = Translator()
11+
12+
engine = pyttsx3.init()
13+
14+
15+
class LanguageTranslator:
16+
def __init__(self, master):
17+
self.master = master
18+
# calling the UI method in the constructor
19+
self.MainWindow()
20+
# calling the Widgets method in the constructor
21+
self.Widgets()
22+
23+
24+
def MainWindow(self):
25+
self.master.geometry('600x430+300+150')
26+
self.master.title('Language Translator')
27+
self.master.resizable(width = 0, height = 0)
28+
# setting the icon for the application
29+
icon = ttk.PhotoImage(file='icon.png')
30+
self.master.iconphoto(False, icon)
31+
32+
33+
def Widgets(self):
34+
# the canvas for containing the other widgets
35+
self.canvas = ttk.Canvas(self.master, width = 600, height = 400)
36+
self.canvas.pack()
37+
# the logo for the application
38+
self.logo = ttk.PhotoImage(file='logo.png').subsample(5, 5)
39+
self.canvas.create_image(75, 70, image = self.logo)
40+
# getting all the languages
41+
language_data = googletrans.LANGUAGES
42+
# getting all the language values using the values() function
43+
language_values = language_data.values()
44+
# converting the languages to a list
45+
languages = list(language_values)
46+
# first combobox for the source language
47+
self.from_language = ttk.Combobox(self.canvas, width = 36, bootstyle = 'primary', values = languages)
48+
self.from_language.current(0)
49+
self.canvas.create_window(150, 140, window = self.from_language)
50+
# loading the arrow icon
51+
self.arrow_icon = ttk.PhotoImage(file='arrows.png')
52+
self.resized_icon = self.arrow_icon.subsample(15, 15)
53+
self.image_label = ttk.Label(self.master, image = self.resized_icon)
54+
self.canvas.create_window(300, 140, window = self.image_label)
55+
# the second combobox for the destination language
56+
self.to_language = ttk.Combobox(self.canvas, width = 36, bootstyle = 'primary', values = languages)
57+
self.to_language.current(21)
58+
self.canvas.create_window(450, 140, window = self.to_language)
59+
# scrollable text for entering input
60+
self.from_text = ScrolledText(self.master, font=("Dotum", 10), width = 30, height = 10)
61+
self.canvas.create_window(150, 250, window = self.from_text)
62+
# scrollable text for output
63+
self.to_text = ScrolledText(self.master, font=("Dotum", 10), width = 30, height = 10)
64+
self.canvas.create_window(450, 250, window = self.to_text)
65+
# loading icons
66+
self.speaker_icon = ttk.PhotoImage(file = 'speaker.png').subsample(5, 4)
67+
self.copy_icon = ttk.PhotoImage(file = 'copy.png').subsample(5, 4)
68+
self.speak_button = ttk.Button(self.master, image = self.speaker_icon, bootstyle='secondary', state=ttk.DISABLED, command = self.speak)
69+
self.canvas.create_window(355, 355, window = self.speak_button)
70+
self.copy_button = ttk.Button(self.master, image = self.copy_icon, bootstyle='secondary', state=ttk.DISABLED, command = self.copy_to_clipboard)
71+
self.canvas.create_window(395, 355, window = self.copy_button)
72+
self.translate_button = ttk.Button(self.master, text = 'Translate', width = 20, bootstyle = 'primary', command = self.translate)
73+
self.canvas.create_window(300, 400, window = self.translate_button)
74+
75+
def translate(self):
76+
try:
77+
# getting source language from first combobox via get()
78+
self.source_language = self.from_language.get()
79+
# getting destination language from first combobox via get()
80+
self.destination_language = self.to_language.get()
81+
# getting every content fronm the first scrolledtext
82+
self.text = self.from_text.get(1.0, ttk.END)
83+
# translating the language
84+
self.translation = translator.translate(self.text, src=self.source_language, dest=self.destination_language)
85+
# clearing the second scrolledtext
86+
self.to_text.delete(1.0, ttk.END)
87+
# inserting translation output in the second scroledtext
88+
self.to_text.insert(ttk.END, self.translation.text)
89+
# activating the speak_button
90+
self.speak_button.configure(state = ttk.ACTIVE)
91+
# activating the copy_button
92+
self.copy_button.configure(state = ttk.ACTIVE)
93+
# handle TypeError using except
94+
except TypeError as e:
95+
showerror(title='Invalid Input', message='Make sure you have entered valid input!')
96+
# handle connection errors
97+
except Exception as e:
98+
showerror(title='Connection Error', message='Make sure you have internet connection!')
99+
100+
def speak(self):
101+
# getting every content from the second scrolledtext
102+
self.text = self.to_text.get(1.0, ttk.END)
103+
# gets the speaking rate
104+
rate = engine.getProperty('rate')
105+
# setting the speaking rate
106+
engine.setProperty('rate', 125)
107+
# getting the available voices
108+
voices = engine.getProperty('voices')
109+
# setting the second voice, the female voice
110+
engine.setProperty('voice', voices[1].id)
111+
# saying the translated text
112+
engine.say(self.text)
113+
# running the speech
114+
engine.runAndWait()
115+
116+
def copy_to_clipboard(self):
117+
# this will create a toast notification object
118+
toast = ToastNotification(
119+
title='Clip Board',
120+
message='Text has been copied to clip board!',
121+
duration=3000,
122+
)
123+
# this will show the notification
124+
toast.show_toast()
125+
# getting all the content from the second scrolledtext
126+
self.text = self.to_text.get(1.0, ttk.END)
127+
# copy to clip board
128+
pyperclip.copy(self.text)
129+
130+
131+
132+
root = ttk.Window(themename="cosmo")
133+
application = LanguageTranslator(root)
134+
root.mainloop()
135+

Diff for: gui-programming/gui-language-translator/icon.png

17.5 KB
Loading

Diff for: gui-programming/gui-language-translator/img001.webp

7 KB
Binary file not shown.

Diff for: gui-programming/gui-language-translator/logo.png

20.2 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ttkbootstrap
2+
googletrans==3.1.0a0
3+
pyttsx3
4+
pyperclip

Diff for: gui-programming/gui-language-translator/speaker.png

782 Bytes
Loading

0 commit comments

Comments
 (0)