2

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.

4
  • Move translator = Translator() below root = Tk(). Commented Jun 7, 2021 at 6:31
  • 1
    Does this answer your question? Googletrans API AttributeError Commented Jun 7, 2021 at 6:44
  • 1
    That's telling you that self.RE_TKK.search(r.text) is returning None. You shouldn't assume the match works before calling .group(1) on the result. Commented Jun 7, 2021 at 13:37
  • What's the fix then? Commented Jun 8, 2021 at 7:46

1 Answer 1

0

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 like

AttributeError: 'NoneType' object has no attribute 'group'

Some use case examples can be found here.

I didn't encounter any issues when I tried google_trans_new. I have tested this on GCP > AI Platform > Notebooks:

!pip install google_trans_new
Collecting google_trans_new
  Downloading google_trans_new-1.1.9-py3-none-any.whl (9.2 kB)
Installing collected packages: google-trans-new
Successfully installed google-trans-new-1.1.9

And try to run

from google_trans_new import google_translator  
translator = google_translator()  
translate_text = translator.translate('hello world',lang_src='en',lang_tgt='zh',pronounce=True)  
print(translate_text)

Output:

['你好,世界 ', None, 'Nǐ hǎo, shìjiè']

Please keep in mind if you would decide to use the newer lib, you should remove old one.

Sign up to request clarification or add additional context in comments.

3 Comments

It isn't working, I've already tried it. And the code also requires 'LANGUAGES'.
This is example of using newer python lib. Above is just example how it would work. If you would like to stay with googletrans there is already opened a Github issue here where using googletrans==4.0.0rc1 worked for many users.
Yes, I have tried that too and it isn't working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.