python上的翻译器不起作用,出现错误说'BaseBlob.translate() got an unexpected keyword argument'


from tkinter.constants import COMMAND
from typing import Text
from PIL import Image, ImageSequence, ImageTk
from googletrans.constants import LANGUAGES
import inflect
from googletrans import Translator
from tkinter.ttk import * 
import textblob

root = tk.Tk()
#Makes the title of the application say INTEGER CONVERTER
root.title('INTEGER CONVERTER | By The Brute Force')
#Sets the size to 1280x720
root.geometry("1280x720")

#Makes the background as an image
back = tk.PhotoImage(file="number.png")
backLabel = tk.Label(root, image=back)
backLabel.place(x=0, y=0, relwidth=1, relheight=1,)
#The Title saying 'INTEGER CONVERTER'
titleText = tk.Label(root, text="INTEGER CONVERTER")
titleText.config(font= ("Wrong Delivery", 50), bg="black", fg="white",)
titleText.pack()
#The subtext saying 'By: The Brute Force'
subText = tk.Label(root, text="BY THE BRUTE FORCE")
subText.config(font= ("Wrong Delivery", 15), bg="black", fg="white")
subText.pack()
subText2 = tk.Label(root, text="ENTER NUMBER BELOW")
subText2.config(font= ("Wrong Delivery", 15), bg="black", fg="white")
subText2.pack(pady=50)
inputBox = tk.Entry(root, bg = "white", fg="black", font =("Wrong Delivery", 30),)
inputBox.place(width=100, height=100)
inputBox.pack()
languages = LANGUAGES
languageList = list(languages.values())
print(languages)
#altResult = tk.Label(root, text='')
#altResult.pack()
global word
def numberConverter():  
global languages
global word
global result
global altResult
converter = inflect.engine()
number = inputBox.get()
word = converter.number_to_words(number)



try:
float(number)


result.config(font=("Wrong Delivery", 30), bg="black", fg="white", text=word.upper(), wraplength=700,  justify="center")

except ValueError:

result.config(font=("Wrong Delivery", 30), bg="black", fg="white", text="PLEASE ENTER A NUMBER!")

#the translator
try:
for key, value in languages.items():
if (value == languageCombo.get()):
toLanguageKey = key
print(toLanguageKey)
textBlobWords = textblob.TextBlob(word)
textBlobWords = textBlobWords.translate(src='en', dest=toLanguageKey)
result.config(font=("Wrong Delivery", 30), bg="black", fg="white", text=textBlobWords)

except Exception as e:
tk.messagebox.showerror("Translator", e)

# translator = Translator(languageCombo.get())
# translation = translator.translate(word)
# result.config(font=("Wrong Delivery", 50), bg="white", fg="white", text=translation)

enterButton = tk.Button(root, font=("Wrong Delivery", 20 ), text="CONVERT!", command=numberConverter)
enterButton.pack(pady=30)
output = tk.Label(root, text='OUTPUT ', font=("Wrong Delivery", 20), bg="black", fg="white")
output.pack()
result = tk.Label(root, text='', bg ="black")
result.pack(pady=20)
languageChoice = tk.Label(root, text='CHOOSE LANGUAGE', font=("Wrong Delivery", 15), bg="black", fg="white")
languageChoice.place(x=20, y=630)


languageCombo = Combobox(root, width=50, value=languageList)
languageCombo.current(21)
languageCombo.place(x=20, y= 660)
theLanguage = languageCombo.get()

root.resizable(False,False) 
root.mainloop()

代码基本上要求用户输入一个数字,并将其转换为数字的拼写,我需要为输出制作一个翻译器。在GUI上,它有一个组合框/下拉框,用户可以选择需要翻译成什么语言。但是代码不起作用。我该怎么办?每当我更改语言时,就会出现一个错误,说"BaseBlob.translate((得到了一个意外的关键字参数"(我这样做是为了让消息框显示一个错误(。

根据TextBlob文档,translate的参数是from_langto,因此您的翻译行应该是:

textBlobWords = textBlobWords.translate(from_lang='en', to=toLanguageKey)

最新更新