I'm trying to write a code for a translation app using python and googletrans API, the code looks fine but there seems to be errors in the tkinter and googletrans libraries. I'll show the code and errors, do help me out if you found the exact reason .
Code:
from tkinter import *
from tkinter import ttk
from googletrans import Translator, LANGUAGES
root = Tk()
root.geometry('1080x400')
root.resizable(0, 0)
root.title("Exposys Labs--Language Translator")
root.config(bg='#99e5f2')
# heading
Label(root, text="LANGUAGE TRANSLATOR", font="arial 20 bold", bg='#99e5f2').pack()
Label(root, text="EXPOSYS LABS", font='arial 20 bold', bg='#99e5f2', width='20').pack(side='bottom')
# INPUT AND OUTPUT TEXT WIDGET
Label(root, text="Enter Text", font='arial 13 bold', bg='white smoke').place(x=200, y=60)
Input_text = Text(root, font='arial 10', height=11, wrap=WORD, padx=5, pady=5, width=60)
Input_text.place(x=30, y=100)
Label(root, text="Translation", font='arial 13 bold', bg='white smoke').place(x=780, y=60)
Output_text = Text(root, font='arial 10', height=11, wrap=WORD, padx=5, pady=5, width=60)
Output_text.place(x=600, y=100)
##################
language = list(LANGUAGES.values())
src_lang = ttk.Combobox(root, values=language, width=22)
src_lang.place(x=20, y=60)
src_lang.set('-Select input language-')
dest_lang = ttk.Combobox(root, values=language, width=22)
dest_lang.place(x=890, y=60)
dest_lang.set('-Select output language-'
'')
# Define function #######
def Translate():
translator = Translator()
translated = translator.translate(text=Input_text.get(1.0, END), src=src_lang.get(), dest=dest_lang.get())
Output_text.delete(1.0, END)
Output_text.insert(END, translated.text)
# Translate Button ########
trans_btn = Button(root, text='Translate', font='arial 12 bold', pady=5, command=Translate, bg='royal blue1',
activebackground='sky blue')
trans_btn.place(x=490, y=180)
root.mainloop()
Errors after running , giving input and clicking on translate:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Safi\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\Safi\PycharmProjects\pythonProject3\main.py", line 41, in Translate
translated = translator.translate(text=Input_text.get(1.0, END), src=src_lang.get(), dest=dest_lang.get())
File "C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\client.py", line 182, in translate
data = self._translate(text, dest, src, kwargs)
File "C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\client.py", line 78, in _translate
token = self.token_acquirer.do(text)
File "C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\gtoken.py", line 195, in do
self._update()
File "C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\gtoken.py", line 63, in _update
code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'
I've tried the latest versions of googletrans and tkinter.
Did you consider using a newer Python Api: google_trans_new instead of googletrans?
I have run some tests with
googletrans
api and got many errors likeSome use case examples can be found here.
I didn't encounter any issues when I tried
google_trans_new
. I have tested this onGCP > AI Platform > Notebooks
:And try to run
Output:
Please keep in mind if you would decide to use the newer lib, you should remove old one.