-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
694 lines (651 loc) · 32.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
from tkinter import *
import customtkinter as ct
import requests
from tkinter import messagebox
from googletrans import Translator
import json
from tkinter import filedialog
import time
import speech_recognition as sr
import subprocess
import os
from pydub import AudioSegment
import pysrt
import sqlite3
import threading
import threading
from sys import platform
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
# Get config prefences from JSON
def get_bg_theme():
with open("theme_config.json", "r") as f:
theme = json.load(f)
return theme["bg_theme"]
# Set themes
ct.set_appearance_mode(get_bg_theme())
translator = Translator()
# Starting main window
one = ct.CTk()
one.title('Translator')
one.geometry(f"{700}x{450}+{570}+{270}")
one.wm_resizable(False, False)
if platform.startswith("win"): one.iconbitmap("icon.ico")
# Appearance theme function
def changeTheme(color):
color = color.lower()
themes_list = ["system", "dark", "light"]
if color in themes_list:
ct.set_appearance_mode(color)
to_change = "bg_theme"
else:
ct.set_default_color_theme(color)
ct.CTkLabel(one, text = "(Restart to take full effect)", font = ("arial", 12)).place(x = 242 , y = 415)
to_change = "default_color"
with open("theme_config.json", "r", encoding="utf8") as f:
theme = json.load(f)
with open("theme_config.json", "w", encoding="utf8") as f:
theme[to_change] = color
json.dump(theme, f, sort_keys = True, indent = 4, ensure_ascii = False)
ct.CTkLabel(one, text = "Appearance Settings", font = ("arial bold", 19)).place(x=490, y=315)
themes_menu = ct.CTkOptionMenu(one, values = ["System", "Dark", "Light"], width = 130, command = changeTheme, corner_radius = 15)
themes_menu.place(x = 520 , y = 355)
themes_menu.set(get_bg_theme().title())
# Main window widgets
ct.CTkLabel(one, text= "Enter The Text:", font=(None, 29, 'bold')).place(x= 85, y= 25)
ct.CTkLabel(one, text= "The Translation:", font=(None, 29, 'bold')).place(x= 397, y= 25)
ct.CTkLabel(one, text= "From:", font=(None, 20)).place(x= 110, y= 235)
ct.CTkLabel(one, text= "To:", font=(None, 20)).place(x= 430, y= 235)
tr_textbox = ct.CTkTextbox(one, width=280, height=150, font=(None, 21), corner_radius=15)
tr_textbox.place(x= 50, y= 70)
to_tr_textbox = ct.CTkTextbox(one, width=280, height=150, font=(None, 21), corner_radius=15)
to_tr_textbox.place(x= 370, y= 70)
to_tr_textbox.configure(state="disabled")
from_lang_list = ["Auto", "Arabic", "Chinese (simplified)", "Chinese (traditional)", "English", "French", "German", "Italian", "Japanese", "Korean", "Portuguese", "Russian", "Spanish", "Turkish"]
from_lang_combo = ct.CTkComboBox(one, width= 90, values= from_lang_list, corner_radius=15)
from_lang_combo.place(x= 170, y= 237)
to_lang_list = ["Arabic", "Chinese (simplified)", "Chinese (traditional)", "English", "French", "German", "Italian", "Japanese", "Korean", "Portuguese", "Russian", "Spanish", "Turkish"]
to_lang_combo = ct.CTkComboBox(one, width= 90, values= to_lang_list, corner_radius= 15)
to_lang_combo.place(x= 465, y= 237)
one.bind('<Return>', lambda event: translate())
# On closing the app
def closing():
one.destroy()
one.protocol("WM_DELETE_WINDOW", closing)
# On back button
def back(window):
one.deiconify()
window.withdraw()
# Text translate function
def translate():
timeout = 1
statues = ""
word = tr_textbox.get("1.0", END)
from_language = from_lang_combo.get()
to_language = to_lang_combo.get()
if word.strip() == "":
return messagebox.showerror('Error', 'Please enter text to translate.')
elif not from_language in from_lang_list:
return messagebox.showerror('Error', 'Please check the language you are translating from.')
elif not to_language in to_lang_list:
return messagebox.showerror('Error', 'Please check the language you are translating to.')
try:
requests.head("http://www.google.com/", timeout=timeout)
statues = "online"
except requests.ConnectionError:
statues= "offline"
except requests.exceptions.Timeout:
statues = "timeout"
if statues == "online":
try:
if from_language == "Auto":
tran = translator.translate(word, dest=to_language)
else:
tran = translator.translate(word, dest=to_language, src=from_language)
except TypeError:
messagebox.showerror('Error', 'Sorry, an unexpected error has occured. Please try again.')
if tran.text == word:
messagebox.showerror('Error', 'Please check your text or language selection.')
else:
to_tr_textbox.configure(state="normal")
to_tr_textbox.delete("1.0", END)
to_tr_textbox.insert(END, tran.text)
to_tr_textbox.configure(state="disabled")
current_string = datetime.now() # Current date in string/readable format
current_timestamp = int(current_string.timestamp()) # Current date as a time stamp
with sqlite3.connect("history.db") as db:
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS history (original TEXT, translation TEXT, time_stamp INTEGER)")
cursor.execute("INSERT INTO history (original, translation, time_stamp) VALUES (?, ?, ?)", (word, tran.text, current_timestamp,))
db.commit()
elif statues == "offline":
messagebox.showerror('Error', 'Please check your connection')
elif statues == "timeout":
messagebox.showerror('Error', 'Connection timeout. Please try again later.')
# Text Translate button
ct.CTkButton(one, text="Translate", corner_radius = 16 , font=(None, 20), width= 190, height=40, command=translate).place(x=255, y= 295)
# Open file translation window
def openfile_tr_win():
one.withdraw() # Withdraw main window
# Open new window
file_tr_win = ct.CTkToplevel()
file_tr_win.title("File Translator")
file_tr_win.geometry(f"{700}x{450}+{570}+{270}")
file_tr_win.resizable(False, False)
if platform.startswith("win"): file_tr_win.iconbitmap("icon.ico")
file_tr_win.protocol("WM_DELETE_WINDOW", closing)
def locate_txt():
filepath = filedialog.askopenfilename(title= "Open a text/subtitles file", filetypes=(("text files", "*.txt"),("subtitles files", "*.srt") , ("all files", "*.*")))
file_path_var.set(filepath)
def file_tr_thread():
threading.Thread(target = tr_file).start()
# File translation function
def tr_file():
timeout = 1
from_language = from_lang_combo.get()
to_language = to_lang_combo.get()
filepath = file_path_var.get()
file_name = os.path.splitext(os.path.basename(filepath))[0]
file_type = os.path.splitext(filepath)[1]
if filepath.strip() == '':
return messagebox.showerror('Error', 'Error: Please type the file path')
elif not from_language in from_lang_list:
return messagebox.showerror('Error', 'Please check the language you are translating from.')
elif not to_language in to_lang_list:
return messagebox.showerror('Error', 'Please check the language you are translating to.')
else:
file_path_entry.configure(state="disabled")
locate_btn.configure(state="disabled")
from_lang_combo.configure(state="disabled")
to_lang_combo.configure(state="disabled")
new_file_radio.configure(state="disabled")
update_file_radio.configure(state="disabled")
replace_file_radio.configure(state="disabled")
tr_btn.configure(state="disabled")
wait_lbl = ct.CTkLabel(file_tr_win, text= "Wait a moment...", font=(None, 15))
wait_lbl.place(x= 313, y= 400)
if file_type == '.srt' and radio_value.get() == 0:
subs = pysrt.open(filepath)
for sub in subs:
translated_text = translator.translate(sub.text, dest=to_language).text
sub.text = translated_text
file_tr_win.update_idletasks()
time.sleep(1)
subs.save(filepath.replace(file_type, "translated_subtitles.srt"))
file_tr_win.update_idletasks()
return messagebox.showinfo('Done','The file has been translated and saved')
elif file_type == '.srt' and radio_value.get() != 0:
return messagebox.showerror('Error', ' You can\'t use this option with a srt file')
else:
with open(filepath, "r+", encoding="utf8") as data:
global lines
lines = data.readlines()
if lines == []:
return messagebox.showerror('Error', 'Error: The file is empty')
tredlines = []
try:
requests.head("http://www.google.com/", timeout=timeout)
statues = "online"
except requests.ConnectionError:
statues= "offline"
except requests.exceptions.Timeout:
statues = "timeout"
if statues == 'online':
if from_language == "Auto":
for line in lines:
a = translator.translate(line, dest=to_language)
tredlines.append(a.text)
file_tr_win.update_idletasks()
time.sleep(0.1)
else:
for line in lines:
a = translator.translate(line, dest=to_language, src= from_language)
tredlines.append(a.text)
file_tr_win.update_idletasks()
time.sleep(0.1)
global string
string = ""
for line in tredlines:
string += (f"{line}\n")
choice_value = radio_value.get()
file_name = os.path.basename(filepath)
file_type = file_name.split(".")[-1].strip()
tr_textbox.configure(state="normal")
tr_textbox.delete("1.0", END)
tr_textbox.insert(END, string)
tr_textbox.configure(state="disabled")
if choice_value == 1:
directory_path = os.path.dirname(filepath)
with open(f"{directory_path}\{file_name}_translated.{file_type}", "w", encoding="utf8") as file:
file.write(string)
elif choice_value == 2:
data.write(f"\n\n{string}")
elif choice_value == 3:
with open(filepath, "w", encoding="utf8") as file:
file.truncate(0)
file.write(string)
elif statues == "offline":
messagebox.showerror('Error', 'Please check your connection')
elif statues == "timeout":
messagebox.showerror('Error', 'Connection timeout. Please try again later.')
file_path_entry.configure(state="normal")
locate_btn.configure(state="normal")
from_lang_combo.configure(state="normal")
to_lang_combo.configure(state="normal")
new_file_radio.configure(state="normal")
update_file_radio.configure(state="normal")
replace_file_radio.configure(state="normal")
tr_btn.configure(state="normal")
wait_lbl.configure(text="")
file_tr_win.bind('<Return>', lambda event: tr_file())
# File translation window widgets
ct.CTkLabel(file_tr_win, text= "Type The File Path (txt/srt):", font=(None, 25)).place(x= 30, y= 30)
file_path_var = StringVar()
file_path_entry = ct.CTkEntry(file_tr_win, textvariable=file_path_var, width=260, height=30, font=(None, 15), corner_radius=15)
file_path_entry.place(x= 30 , y= 75)
locate_btn = ct.CTkButton(file_tr_win, text= 'Locate', corner_radius=15, font=(None, 17), width=60, command=locate_txt)
locate_btn.place(x=300, y=75)
ct.CTkLabel(file_tr_win, text= "From:", font=(None, 20)).place(x= 40, y=130)
ct.CTkLabel(file_tr_win, text= "To:", font=(None, 20)).place(x= 220 , y= 130)
from_lang_combo = ct.CTkComboBox(file_tr_win, width= 90, corner_radius=15, values= from_lang_list)
from_lang_combo.place(x=100, y=130)
to_lang_combo = ct.CTkComboBox(file_tr_win, width= 90, values= to_lang_list, corner_radius=15)
to_lang_combo.place(x=255, y=130)
radio_value = IntVar()
new_file_radio = ct.CTkRadioButton(file_tr_win, variable=radio_value, value=1, text ="New file with translation", font=(None, 20))
new_file_radio.place(x=40, y=185)
update_file_radio = ct.CTkRadioButton(file_tr_win, variable=radio_value, value=2, text ="Update file with translation", font=(None, 20))
update_file_radio.place(x=40, y=220)
replace_file_radio = ct.CTkRadioButton(file_tr_win, variable=radio_value, value=3, text ="Replace file with translation", font=(None, 20))
replace_file_radio.place(x=40, y=255)
tr_btn = ct.CTkButton(file_tr_win, text="Translate", font=(None, 20), width= 190, height=40, corner_radius=15, command=file_tr_thread)
tr_btn.place(x= 250, y= 330)
ct.CTkButton(file_tr_win, text= 'Back', font=(None, 20), command=lambda: back(file_tr_win), corner_radius=15, width=70).place(x=20, y= 400)
ct.CTkLabel(file_tr_win, text= "The Translation:", font=(None, 25,'bold')).place(x= 440, y=30 )
tr_textbox = ct.CTkTextbox(file_tr_win, width=280, height=230, font=(None, 21), corner_radius=15)
tr_textbox.place(x= 400, y= 75)
tr_textbox.configure(state="disabled")
# Audio translation window
def openaudiotr():
one.withdraw() # Main withdraw
# Window start
audio_tr_win = ct.CTkToplevel()
audio_tr_win.title("Audio Translator")
audio_tr_win.geometry(f"{700}x{450}+{570}+{270}")
audio_tr_win.resizable(False, False)
if platform.startswith("win"): audio_tr_win.iconbitmap("icon.ico")
audio_tr_win.protocol("WM_DELETE_WINDOW", closing)
def locate():
filepath = filedialog.askopenfilename(title= "Open a wav/mp4 file", filetypes=(("Video files", "*.mp4"), ("Audio files", "*.wav"),("Audio files", "*.mp3"), ("all files", "*.*")))
path_var.set(filepath)
def audio_tr_thread():
threading.Thread(target = audio_tr_winans).start()
# Audio translation function
def audio_tr_winans():
checkvalue = radio_value2.get()
print(radio_value2.get())
r = sr.Recognizer()
timeout = 1
try:
requests.head("http://www.google.com/", timeout=timeout)
statues = "online"
except requests.ConnectionError:
statues= "offline"
except requests.exceptions.Timeout:
statues = "timeout"
from_language = from_lang_combo.get()
to_language = to_lang_combo.get()
data = ""
audio_tr = ''
filepath = path_var.get()
if filepath.strip() == "":
return messagebox.showerror('Error', 'Please type the file path.')
elif not from_language in from_lang_list:
return messagebox.showerror('Error', 'Please check the language you are translating from.')
elif not to_language in to_lang_list:
return messagebox.showerror('Error', 'Please check the language you are translating to.')
if statues == 'online':
file_name = os.path.splitext(os.path.basename(filepath))[0]
file_type = os.path.splitext(filepath)[1]
if file_type == ".mp4":
pass
elif file_type == ".wav":
pass
elif file_type == ".mp3":
pass
else:
return messagebox.showerror('Error', 'File format is not supported.')
if file_type == '.wav' or file_type == '.mp3' and checkvalue ==2:
return messagebox.showerror('Error', ' You can\'t use this option with an audio file')
entry_a.configure(state="disabled")
locate_btn.configure(state="disabled")
from_lang_combo.configure(state="disabled")
to_lang_combo.configure(state="disabled")
radio_a2.configure(state="disabled")
radio_a3.configure(state="disabled")
tr_btn.configure(state="disabled")
wait_lbl = ct.CTkLabel(audio_tr_win, text= "Wait a moment...", font=(None, 15))
wait_lbl.place(x= 313, y= 400)
if file_type == ".mp3":
input = filepath
output = "audio_in_wav.wav"
audio = AudioSegment.from_mp3(input)
audio.export(output, format="wav")
with sr.AudioFile(output) as source:
audio_data = r.record(source)
adata = r.recognize_google(audio_data)
data = f"{adata}"
if from_language == "Auto":
audio_tr = translator.translate(data, dest=to_language)
else:
audio_tr = translator.translate(data, dest=to_language , src=from_language)
string = ""
string += audio_tr.text
tr_textbox.configure(state="normal")
tr_textbox.delete("1.0", END)
tr_textbox.insert(END, string)
tr_textbox.configure(state="disabled")
if checkvalue == 1:
save = open(f"{file_name}.txt", "x")
save.write(string)
if file_type == ".wav":
with sr.AudioFile(file_name) as source:
audio_data = r.record(source)
adata = r.recognize_google(audio_data)
data = f"{adata}"
if from_language == "Auto":
audio_tr = translator.translate(data, dest=to_language)
else:
audio_tr = translator.translate(data, dest=to_language , src=from_language)
string = ""
string += audio_tr.text
tr_textbox.configure(state="normal")
tr_textbox.delete("1.0", END)
tr_textbox.insert(END, string)
tr_textbox.configure(state="disabled")
if checkvalue == 1:
save = open(f"{file_name}.txt", "x")
save.write(string)
elif file_type == ".mp4":
if checkvalue == 2:
cmd = f'ffmpeg -i "{filepath}" -vn -acodec pcm_s16le -ar 44100 -ac 2 "{filepath.replace(file_type, "_audio.wav")}"'
subprocess.call(cmd, shell=True)
audio = filepath.replace(file_type, "_audio.wav")
r = sr.Recognizer()
with sr.AudioFile(audio) as source:
audio = r.listen(source)
try:
text = r.recognize_google(audio)
except:
text = ""
subs = pysrt.SubRipFile()
sentences = text.split(". ")
start = 0
end = 0
string = ""
for i, s in enumerate(sentences):
duration = len(s.split()) * 0.6
end = start + duration
item = pysrt.SubRipItem()
item.index = i + 1
item.start.seconds = start
item.end.seconds = end
translation = translator.translate(s, dest=to_language , src=from_language)
item.text = translation.text
string = f"{translation.text}\n"
subs.append(item)
start = end
subs.save(filepath.replace(file_type, "translated_subtitles.srt"), encoding='utf-8')
os.remove(filepath.replace(file_type, "_audio.wav"))
tr_textbox.configure(state="normal")
tr_textbox.delete("1.0", END)
tr_textbox.insert(END, string)
tr_textbox.configure(state="disabled")
else:
cmd = f'ffmpeg -i "{filepath}" -vn -acodec pcm_s16le -ar 44100 -ac 2 "{filepath.replace(file_type, "_audio.wav")}"'
subprocess.call(cmd, shell=True)
audio = filepath.replace(file_type, "_audio.wav")
with sr.AudioFile(audio) as source:
audio_data = r.record(source)
adata = r.recognize_google(audio_data)
data = f"{adata}"
if from_language == "Auto":
audio_tr = translator.translate(data, dest=to_language)
else:
audio_tr = translator.translate(data, dest=to_language , src=from_language)
string = ""
string += audio_tr.text
tr_textbox.configure(state="normal")
tr_textbox.delete("1.0", END)
tr_textbox.insert(END, string)
tr_textbox.configure(state="disabled")
directory_path = os.path.dirname(filepath)
with open(f"{directory_path}/{file_name}.txt", "w") as f:
f.write(string)
elif statues == 'offline':
messagebox.showerror('Error', 'Error: Please check your connection')
elif statues == "timeout":
messagebox.showerror('Error', 'Connection timeout. Please try again later.')
entry_a.configure(state="normal")
locate_btn.configure(state="normal")
from_lang_combo.configure(state="normal")
to_lang_combo.configure(state="normal")
radio_a2.configure(state="normal")
radio_a3.configure(state="normal")
tr_btn.configure(state="normal")
wait_lbl.configure(text="")
# Audio translation widgets
ct.CTkLabel(audio_tr_win, text= "Type The File Path :", font=(None, 23)).place(x= 30, y= 30)
ct.CTkLabel(audio_tr_win, text= "(mp4/wav/mp3)", font=(None, 17)).place(x= 235, y= 32)
path_var = StringVar()
entry_a = ct.CTkEntry(audio_tr_win, textvariable=path_var, width=260, height=30, font=(None, 15), corner_radius=15)
entry_a.place(x= 30 , y= 75)
locate_btn = ct.CTkButton(audio_tr_win, text= 'Locate', corner_radius=15, font=(None, 17), width=60, command=locate)
locate_btn.place(x=300, y=75)
ct.CTkLabel(audio_tr_win, text= "From:", font=(None, 20)).place(x= 35, y=125)
ct.CTkLabel(audio_tr_win, text= "To:", font=(None, 20)).place(x= 215 , y= 125)
from_lang_combo = ct.CTkComboBox(audio_tr_win, width= 90, corner_radius=15, values= from_lang_list)
to_lang_combo= ct.CTkComboBox(audio_tr_win, width= 90, values= to_lang_list, corner_radius=15)
from_lang_combo.place(x=97, y=125)
to_lang_combo.place(x=252, y=125)
radio_value2 = IntVar()
radio_a2 = ct.CTkRadioButton(audio_tr_win, variable=radio_value2, value=1, text ="Save the translation in a txt file",corner_radius=15, font=(None, 20))
radio_a2.place(x=35, y=180)
radio_a3 = ct.CTkRadioButton(audio_tr_win, variable=radio_value2, value=2,text ="Save the translation in a srt file", corner_radius=15, font=(None, 20))
radio_a3.place(x=35, y=215)
tr_btn = ct.CTkButton(audio_tr_win, text="Translate", font=(None, 20), width= 190, height=40, corner_radius=15, command=audio_tr_thread)
tr_btn.place(x= 270, y= 350)
ct.CTkButton(audio_tr_win, text= 'Back', font=(None, 20), command=lambda: back(audio_tr_win), corner_radius=15, width=70).place(x=20, y= 400)
ct.CTkLabel(audio_tr_win, text= "The Translation:", font=(None, 25,'bold')).place(x= 440, y=30 )
tr_textbox = ct.CTkTextbox(audio_tr_win, width=280, height=230, font=(None, 21), corner_radius=15)
tr_textbox.place(x= 400, y= 80)
tr_textbox.configure(state="disabled")
# Image translation window
def openimagetr():
one.withdraw() # Main withdraw
# Window start
image_tr_win = ct.CTkToplevel()
image_tr_win.title("Image Translator")
image_tr_win.geometry(f"{700}x{450}+{570}+{270}")
image_tr_win.resizable(False, False)
if platform.startswith("win"): image_tr_win.iconbitmap("icon.ico")
image_tr_win.protocol("WM_DELETE_WINDOW", closing)
# Locate
def locate():
filepath = filedialog.askopenfilename(title= "Open a jpg/png file", filetypes=(("Image files", "*.jpg"), ("Image files", "*.png"), ("all files", "*.*")))
path_var.set(filepath)
def img_thread():
threading.Thread(target = img_tr).start()
# Image translation
def img_tr():
timeout = 1
from_language = from_lang_combo.get()
to_language = to_lang_combo.get()
filepath = path_var.get()
file_name = os.path.splitext(os.path.basename(filepath))[0]
file_type = os.path.splitext(filepath)[1]
if filepath.strip() == '':
return messagebox.showerror('Error', 'Error: Please type the file path')
elif not from_language in from_lang_list:
return messagebox.showerror('Error', 'Please check the language you are translating from.')
elif not to_language in to_lang_list:
return messagebox.showerror('Error', 'Please check the language you are translating to.')
else:
if file_type != ".png" and file_type != ".jpg" and file_type != ".jpeg":
messagebox.showerror("Error", "This format is not supported.")
else:
entry_a.configure(state="disabled")
locate_btn.configure(state="disabled")
from_lang_combo.configure(state="disabled")
to_lang_combo.configure(state="disabled")
radio_a2.configure(state="disabled")
radio_a3.configure(state="disabled")
tr_btn.configure(state="disabled")
wait_lbl = ct.CTkLabel(image_tr_win, text= "Wait a moment...", font=(None, 15))
wait_lbl.place(x= 313, y= 400)
# Getting the text from the image
def get_text(api_key):
api_url = 'https://api.api-ninjas.com/v1/imagetotext'
image_file_descriptor = open(filepath, 'rb')
headers = {"X-Api-Key": api_key}
files = {'image': image_file_descriptor}
response = requests.post(api_url, files=files, headers=headers)
text = response.json()
string = ""
print(text)
for word in text:
string += word['text'] + " "
return string
try: text = get_text(os.getenv("K+vwIRZM3NgfQP2nOBmZqg==0c3QxNHh4dbT9Hwr"))
except TypeError: text = get_text(os.getenv("API_KEY2"))
except: return messagebox.showerror("Sorry, an unexpected error has occured.")
# Translating
try: requests.head("http://www.google.com/", timeout=timeout)
except requests.ConnectionError: return messagebox.showerror('Error', 'Please check your connection')
except requests.exceptions.Timeout: return messagebox.showerror('Error', 'Connection timeout. Please try again later.')
if from_language == "Auto": translation = translator.translate(text, dest=to_language).text
else: translation = translator.translate(text, dest=to_language, src=from_language).text
# Saving the translation
tr_textbox.configure(state="normal")
tr_textbox.delete("1.0", END)
tr_textbox.insert(END, translation)
tr_textbox.configure(state="disabled")
directory_path = os.path.dirname(filepath)
if radio_value2.get() == 1:
with open(f"{directory_path}\{file_name}_translated.txt", "w", encoding="utf8") as file:
file.write(translation)
elif radio_value2.get() == 2:
with open(f"{directory_path}\{file_name}_translated.txt", "w", encoding="utf8") as file:
file.write(f"{text}\n\n{translation}")
entry_a.configure(state="normal")
locate_btn.configure(state="normal")
from_lang_combo.configure(state="normal")
to_lang_combo.configure(state="normal")
radio_a2.configure(state="normal")
radio_a3.configure(state="normal")
tr_btn.configure(state="normal")
wait_lbl.configure(text="")
image_tr_win.bind('<Return>', lambda event: img_tr())
# Image translation widgets
ct.CTkLabel(image_tr_win, text= "Type The File Path (jpg/png):", font=(None, 25)).place(x= 30, y= 30)
path_var = StringVar()
entry_a = ct.CTkEntry(image_tr_win, textvariable=path_var, width=260, height=30, font=(None, 15), corner_radius=15)
entry_a.place(x= 30 , y= 75)
locate_btn = ct.CTkButton(image_tr_win, text= 'Locate', corner_radius=15, font=(None, 17), width=60, command=locate)
locate_btn.place(x=300, y=75)
ct.CTkLabel(image_tr_win, text= "From:", font=(None, 20)).place(x= 35, y=125)
ct.CTkLabel(image_tr_win, text= "To:", font=(None, 20)).place(x= 215 , y= 125)
from_lang_combo = ct.CTkComboBox(image_tr_win, width= 90, corner_radius=15, values= from_lang_list)
to_lang_combo= ct.CTkComboBox(image_tr_win, width= 90, values= to_lang_list, corner_radius=15)
from_lang_combo.place(x=97, y=125)
to_lang_combo.place(x=252, y=125)
radio_value2 = IntVar()
radio_a2 = ct.CTkRadioButton(image_tr_win, variable=radio_value2, value=1, text ="Save the translation in a txt file",corner_radius=15, font=(None, 20))
radio_a2.place(x=35, y=180)
radio_a3 = ct.CTkRadioButton(image_tr_win, variable=radio_value2, value=2, text ="Save the original text and the\ntranslation in a txt file",corner_radius=15, font=(None, 20))
radio_a3.place(x=35, y=215)
tr_btn = ct.CTkButton(image_tr_win, text="Translate", font=(None, 20), width= 190, height=40, corner_radius=15, command=img_thread)
tr_btn.place(x= 270, y= 350)
ct.CTkButton(image_tr_win, text= 'Back', font=(None, 20), command=lambda: back(image_tr_win), corner_radius=15, width=70).place(x=20, y= 400)
ct.CTkLabel(image_tr_win, text= "The Translation:", font=(None, 25,'bold')).place(x= 440, y=30 )
tr_textbox = ct.CTkTextbox(image_tr_win, width=280, height=230, font=(None, 21), corner_radius=15)
tr_textbox.place(x= 400, y= 80)
tr_textbox.configure(state="disabled")
# History window
def openhistory():
one.withdraw() # Main withdraw
# Window start
history_win = ct.CTkToplevel()
history_win.title("Translation History")
history_win.geometry(f"{700}x{450}+{570}+{270}")
history_win.resizable(False, False)
if platform.startswith("win"): history_win.iconbitmap("icon.ico")
history_win.protocol("WM_DELETE_WINDOW", closing)
def delete_history():
if os.path.isfile("history.db") == False: messagebox.showerror("Error", "You didn't translate anything yet.")
ask = messagebox.askokcancel("Are you sure?", "Your whole history will be deleted, you wanna continue?")
if ask == True:
try:
with sqlite3.connect("history.db") as db:
cursor = db.cursor()
cursor.execute("DROP TABLE history")
db.commit()
history_textbox.configure(state="normal")
history_textbox.delete("1.0", END)
history_textbox.configure(state="disabled")
except sqlite3.OperationalError: messagebox.showerror("Error", "Your history is already cleared.")
# History widgets
history_textbox = ct.CTkTextbox(history_win, width=679, height=370, font=(None, 20), corner_radius=15)
history_textbox.place(x= 10, y= 10)
history_textbox.configure(state="disabled")
ct.CTkButton(history_win, text= 'Back', font=(None, 20), command=lambda: back(history_win), corner_radius=15, width=70).place(x=20, y= 400)
ct.CTkButton(history_win, text= 'Delete History', font=(None, 20), command=delete_history, corner_radius=15, width=70, fg_color="red").place(x=525, y= 400)
# Show history
if os.path.isfile("history.db") == False: return
try:
with sqlite3.connect("history.db") as db:
cursor = db.cursor()
cursor.execute("SELECT original, translation, time_stamp FROM history")
data = cursor.fetchall()
data = sorted(data, key=lambda x: x[2], reverse=True)
for row in data:
row = list(row)
original = str(row[0]).replace("\n", "")
translation = str(row[1])
time_stamp = int(row[2])
date = datetime.fromtimestamp(time_stamp).strftime("%B %d, %Y %I:%M %p")
history_textbox.configure(state="normal")
history_textbox.insert(END, f"{date} --- {original} = {translation}\n")
history_textbox.configure(state="disabled")
history_textbox.configure(state="disabled")
except sqlite3.OperationalError: pass
def OpenAbout():
one.withdraw() # Main withdraw
# Window start
About_win = ct.CTkToplevel()
About_win.title("About Us")
About_win.geometry(f"{700}x{450}+{570}+{270}")
About_win.resizable(False, False)
if platform.startswith("win"): About_win.iconbitmap("icon.ico")
About_win.protocol("WM_DELETE_WINDOW", closing)
#About Us Widgets
ct.CTkLabel(About_win, text="Mohamed Ayman :", font=(None, 22)).place(x =15,y=25)
About_textbox1 = ct.CTkTextbox(About_win, width=679, height=135, font=(None, 20), corner_radius=15)
About_textbox1.place(x= 15, y=65 )
About_textbox1.insert(END, "Github: https://github.com/Shinobi7k\nTwitter: https://twitter.com/Shinobi7k\nGmail: [email protected]")
About_textbox1.configure(state="disabled")
ct.CTkLabel(About_win, text="Ahmed Ibrahim:", font=(None, 22)).place(x =15,y=215)
About_textbox2 = ct.CTkTextbox(About_win, width=679, height=135, font=(None, 20), corner_radius=15)
About_textbox2.place(x= 15, y=255 )
About_textbox2.insert(END, "Github: https://github.com/Ahmed5431\nGmail: [email protected]")
About_textbox2.configure(state="disabled")
About_textbox2.configure(state="disabled")
ct.CTkButton(About_win, text= 'Back', font=(None, 20), command=lambda: back(About_win), corner_radius=15, width=70).place(x=20, y= 400)
# Other windows open buttons
ct.CTkButton(one, text="Translate File", font=(None, 18), width= 130, height=30, command=openfile_tr_win, corner_radius= 15).place(x=20, y= 310)
ct.CTkButton(one, text="Translate Image", font=(None, 18), width= 130, height=30, command=openimagetr, corner_radius= 15).place(x=20, y= 355)
ct.CTkButton(one, text="Translate Audio/Video", font=(None, 18), width= 130, height=30, command=openaudiotr , corner_radius= 15).place(x=20, y= 400)
ct.CTkButton(one, text="Translation History", font=(None, 18), width= 130, height=30, command=openhistory, corner_radius= 15).place(x=270, y= 400)
ct.CTkButton(one, text= "About Us", font=(None, 18), width= 130, height= 30, command=OpenAbout,corner_radius=15).place(x= 520,y=400)
one.mainloop()