簡體   English   中英

Tkinter 回調和 googletrans api 中的異常

[英]Exception in Tkinter callback and googletrans api

我正在嘗試使用 python 和 googletrans API 為翻譯應用程序編寫代碼,代碼看起來不錯,但 tkinter 和 googletrans 庫中似乎存在錯誤。 我將顯示代碼和錯誤,如果您找到確切原因,請幫助我。

代碼:

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:\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'

我已經嘗試過最新版本的 googletrans 和 tkinter。

您是否考慮過使用更新的 Python Api: google_trans_new而不是googletrans

我已經用googletrans api 進行了一些測試,並遇到了很多錯誤,例如

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)

Output:

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

請記住,如果您決定使用較新的庫,則應刪除舊庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM