简体   繁体   English

Tkinter 回调和 googletrans api 中的异常

[英]Exception in Tkinter callback and googletrans api

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.我正在尝试使用 python 和 googletrans API 为翻译应用程序编写代码,代码看起来不错,但 tkinter 和 googletrans 库中似乎存在错误。 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.我已经尝试过最新版本的 googletrans 和 tkinter。

Did you consider using a newer Python Api: google_trans_new instead of googletrans ?您是否考虑过使用更新的 Python Api: google_trans_new而不是googletrans

I have run some tests with googletrans api and got many errors like我已经用googletrans api 进行了一些测试,并遇到了很多错误,例如

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 .我尝试google_trans_new时没有遇到任何问题。 I have tested this on GCP > AI Platform > Notebooks :我已经在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: 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.请记住,如果您决定使用较新的库,则应删除旧库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM