Skip to content

Commit

Permalink
Update GTranslator.py
Browse files Browse the repository at this point in the history
Version: 2.1.2
Many things changed.
  • Loading branch information
PAIREN1383 authored Jul 26, 2024
1 parent 9a3c48c commit c39b8f7
Showing 1 changed file with 80 additions and 60 deletions.
140 changes: 80 additions & 60 deletions GTranslator/GTranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
########################## Front-End ##########################
window = Tk()
window.title("GTranslator")
window.geometry("350x350")
window.geometry("350x400")
icon = PhotoImage(file=".\\gt_icon.png")
window.call("wm", "iconphoto", window._w, icon)
entxt_font = font.Font(family="tahoma", size=10, weight="bold")
Expand All @@ -27,11 +27,12 @@ def help_box():
\n[2] Enter the word or sentence in the entry and press 'Enter' or click the Translate button. \
\n\n\nPublisher: MohammadAli \
\nGitHub: https://github.com/pairen1383/ \
\nVersion: 1.0.1")
\nVersion: 2.1.2")


def show_searchbar():
global ent
global ent, on_off

lb = Label(window, justify="left", text="Text: ")
lb.place(x=0, y=2)

Expand All @@ -48,75 +49,81 @@ def show_searchbar():
hbtn = Button(window, text="?", width=2, command=help_box)
hbtn.place(x=325, y=0)


# We clear the window so that there is no text on the page that goes under another text.
def clear_window():
for widget in window.winfo_children():
widget.destroy()
on_off = BooleanVar()
checkbox = Checkbutton(window, variable=on_off, onvalue=True, offvalue=False)
checkbox.place(x=2, y=23)

cblb = Label(window, justify="left", text="Always use offline pronunciation.")
cblb.place(x=20, y=25)


class Label_Text_area:
def __init__(self, wid_root, lb_x, lb_y, lb_txt, lb_justify, ta_scrollbar_x_margin, ta_width, ta_height, ta_font, ta_data, ta_justify) -> None:
self.root = wid_root
self.lb_x = lb_x
self.lb_y = lb_y
self.lb_txt = lb_txt
self.lb_justify = lb_justify
self.margin = ta_scrollbar_x_margin
self.ta_width = ta_width
self.ta_height = ta_height
self.ta_font = ta_font
self.ta_justify = ta_justify
self.ta_data = ta_data

if self.lb_txt != "":
txt_lb = Label(self.root, justify=self.lb_justify, text=self.lb_txt)
txt_lb.place(x=self.lb_x, y=self.lb_y)
self.txt_area = Text(self.root, width=self.ta_width, height=self.ta_height, font=self.ta_font, wrap=WORD)
self.sc = Scrollbar(self.root)
self.sc.place(x=self.lb_x + self.margin + self.ta_width * 8, y=self.lb_y + 20) # Approximately 1 unit of width equals 8 units of x-position
self.sc.config(command=self.txt_area.yview)
self.txt_area.tag_configure("tg_name", justify=self.ta_justify) # Persian text is written from right to left.
self.txt_area.insert(INSERT, self.ta_data)
self.txt_area.tag_add("tg_name", 1.0, "end") # Add tag from beginning (Line 1 and index 0) to end.
self.txt_area.place(x=self.lb_x, y=self.lb_y + 20) # The text area and scroll bar should be 20 y-position down.

def update_ta(self, new_data):
self.ta_data = new_data
self.txt_area.delete("1.0", END) # Clear the Textarea.
self.txt_area.insert(INSERT, self.ta_data)
self.txt_area.tag_add("tg_name", 1.0, "end") # It must be applied every time.


def basic_view():
global trtxt, other_trtxt, intxt, other_intxt
show_searchbar()
trtxt = Label_Text_area(window, 2, 50, "Translated text:", "left", -40, 46, 3, fatxt_font, "", "right")
other_trtxt = Label_Text_area(window, 2, 145, "Other translations:", "left", -40, 46, 3, fatxt_font, "", "right")
intxt = Label_Text_area(window, 2, 240, "Synonyms:", "left", 8, 40, 3, entxt_font, "", "left")
other_intxt = Label_Text_area(window, 2, 320, "Definition:", "left", 8, 40, 3, entxt_font, "", "left")


########################## Back-End ##########################
# Persian text is written from right to left.
def justify_txt(tr_text, in_text, zoom, extra=[]):
global ent, trtxt
clear_window()
show_searchbar()
global ent, trtxt, other_trtxt, intxt, other_intxt
ent.delete(0, END) # Clear the entry.
ent.insert(0, in_text) # Displays the entered text.
trtxt = Text(window, width=46, height=3, font=fatxt_font, wrap=WORD)
sc1 = Scrollbar(window)
sc1.place(x=330, y=35)
sc1.config(command=trtxt.yview)
trtxt.delete("1.0", END) # Clear the Textarea.
trtxt.tag_configure("rt", justify="right") # Persian text is written from right to left.
trtxt.insert(INSERT, tr_text)
trtxt.tag_add("rt", 1.0, "end") # Add tag from beginning (Line 1 and index 0) to end.
trtxt.place(x=2, y=35)

trtxt.update_ta(tr_text)
if extra != []:
trlb = Label(window, justify="left", text="Other translations:")
trlb.place(x=0, y=105)
other_trtxt = Text(window, width=46, height=3, font=fatxt_font, wrap=WORD)
sc2 = Scrollbar(window)
sc2.place(x=330, y=125)
sc2.config(command=other_trtxt.yview)
other_trtxt.delete("1.0", END)
for obj in extra[0]:
obj.replace("{", "").replace("}", "") # Remove unwanted characters.
obj.strip()
other_trtxt.tag_configure("rt", justify="right")
other_trtxt.insert(INSERT, " ،".join(extra[0])) # Separate it.
other_trtxt.tag_add("rt", 1.0, "end")
other_trtxt.place(x=2, y=125)

inlb1 = Label(window, justify="left", text="Synonyms:")
inlb1.place(x=0, y=195)
intxt = Text(window, width=40, height=3, font=entxt_font, wrap=WORD)
sc3 = Scrollbar(window)
sc3.place(x=330, y=215)
sc3.config(command=intxt.yview)
intxt.delete("1.0", END)
intxt.insert("1.0", ", ".join(extra[1]))
intxt.place(x=2, y=215)

inlb2 = Label(window, justify="left", text="Definition:")
inlb2.place(x=0, y=270)
other_intxt = Text(window, width=40, height=3, font=entxt_font, wrap=WORD)
sc4 = Scrollbar(window)
sc4.place(x=330, y=290)
sc4.config(command=other_intxt.yview)
other_intxt.delete("1.0", END)
other_intxt.insert("1.0", extra[2])
other_intxt.place(x=2, y=290)
ent.delete(0, END)
ent.insert(0, in_text)
other_trtxt.update_ta(" ،".join(extra[0]))
intxt.update_ta(", ".join(extra[1]))
other_intxt.update_ta(extra[2])
else:
other_trtxt.update_ta("")
intxt.update_ta("")
other_intxt.update_ta("")

if zoom: # Appears when the program is minimized or placed under other windows.
window.attributes("-topmost", True)
window.state("normal")
window.attributes("-topmost", False) # Do not delete this line because the program will never go under another window.


########################## Back-End ##########################
def translate_txt(txt, show_win=False):
translator = Translator()
flag = False
Expand All @@ -141,11 +148,18 @@ def translate_txt(txt, show_win=False):


def play_voice():
global ent
global ent, on_off
get_txt = ent.get().strip() # Delete unwanted spaces after and before the text or word.
if get_txt == "":
messagebox.showerror("GTranslator", "Error: \nEnter a sentence or word!")
return None
if on_off.get():
play_voice_offline(get_txt)
else:
play_voice_online(get_txt)


def play_voice_online(get_txt):
try:
tts = gTTS(get_txt, timeout=3)
tts.save(".\\never_save.mp3")
Expand All @@ -162,6 +176,14 @@ def play_voice():
messagebox.showerror("GTranslator", f"Error: \n{err}")


def play_voice_offline(get_txt):
# This is an offline voice maker.
engine = init()
engine.setProperty("rate", 120) # Text or word reading speed.
engine.say(get_txt)
engine.runAndWait()


# Ctrl+C handler.
def clip_key():
sleep(0.2) # The text must be saved to the clipboard.
Expand All @@ -180,10 +202,8 @@ def ent_key():
translate_txt(ent.get().strip())


########################## Start ##########################
add_hotkey("ctrl+c", clip_key)
add_hotkey("enter", ent_key)


########################## Start ##########################
show_searchbar()
basic_view()
window.mainloop()

0 comments on commit c39b8f7

Please sign in to comment.