Tkinter回调和googletrans-api出现异常



我正在尝试使用python和googletrans API为翻译应用程序编写代码,代码看起来很好,但tkinter和googlerans库中似乎有错误。我会显示代码和错误,如果你找到确切的原因,一定要帮我。

代码:

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()

运行、输入和点击翻译后出现错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersSafiAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 1892, in __call__
return self.func(*args)
File "C:UsersSafiPycharmProjectspythonProject3main.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:UsersSafiPycharmProjectspythonProject3venvlibsite-packagesgoogletransclient.py", line 182, in translate
data = self._translate(text, dest, src, kwargs)
File "C:UsersSafiPycharmProjectspythonProject3venvlibsite-packagesgoogletransclient.py", line 78, in _translate
token = self.token_acquirer.do(text)
File "C:UsersSafiPycharmProjectspythonProject3venvlibsite-packagesgoogletransgtoken.py", line 195, in do
self._update()
File "C:UsersSafiPycharmProjectspythonProject3venvlibsite-packagesgoogletransgtoken.py", line 63, in _update
code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'

我试过最新版本的谷歌翻译和tkinter。

您是否考虑使用较新的Python Api:google_trans_new而不是googletrans?

我已经用googletransAPI运行了一些测试,并得到了许多错误,如

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

一些用例示例可以在这里找到。

当我尝试google_trans_new时,没有遇到任何问题。我已经在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

并尝试运行

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)

输出:

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

请记住,如果您决定使用较新的库,您应该删除旧的库。

相关内容

  • 没有找到相关文章

最新更新