I make a translator app using tkinter and googletrans
but when i run it, the googletrans only return 1 word here is my full code (main.py)
#Importing modules
from tkinter import *
from googletrans import Translator
##Main Script
#Main Window
Window = Tk()
Window.geometry("400x700")
Window.resizable(False, False)
Window.configure(bg="#e1f4f3")
#Defs
def translate():
ans = inp.get(1.0)
trans = Translator()
t = trans.translate(
ans, src="id", dest="en"
)
trans_txt.config(state="normal")
trans_txt.delete(END, "end")
trans_txt.insert(END, t.text)
trans_txt.config(state="normal")
#Widgets
#main frame
main = Frame(Window, width=300, height=500, bg="#00d1ff", bd=0, highlightthickness=0)
#entries
#input user
inp = Text(main, bd=0, highlightthickness=0, font=("Gotham Book", 20))
inp.pack_propagate(0)
#translate btn
translate_btn = Button(main, bd=0, highlightthickness=0, font=("Gotham Book", 20), text="Translate", command=translate)
translate_btn.configure(highlightbackground="light blue")
translate_btn.pack_propagate(0)
#translation
trans_txt = Text(main, bd=0, highlightthickness=0, font=("Gotham Book", 20))
trans_txt.config(state="disabled")
trans_txt.pack_propagate(0)
#Packs
main.place(anchor="c", rely=.5, relx=.5)
inp.place(x=150, anchor="c", y=100, height=90, width=250)
translate_btn.place(x=150, anchor="c", y=250)
trans_txt.place(x=150, anchor="c", y=400, height=90, width=250)
#Window.mainloop()
Window.mainloop()
the main.py returns
H
when i type "Hai". in the inp widget
but in the other code file (trans_test.py) it returns the right translated word here is trans_test.py full code
#Importing modules
from googletrans import Translator
#trans()
def trans(text):
trans = Translator()
t = trans.translate(
text, src="id", dest="en"
)
return t.text
#test
print(trans("Hai"))
the trans_test.py returns
Hi
Thank you if someone answer this :)
The reason why you getting only
h
is that yourtranslate
function is trying to translateh
which ish
. At lineans = inp.get(1.0)
, you are just getting the character at row 1 and col 0 aka1.0
, you need to get the complete text which can be done by doingans = inp.get(1.0, 'end')
.Also, I want to address
trans_txt.delete(END, "end")
if you are trying to delete the text completely then you need to delete from staring index to the end index so it should be.Both
END
or"end"
are the same values don't get confused.Also if you are trying to make the 2nd Text widget a read-only Text then you just need to disable the state after inserting.
Improved
translate
function