Home > Software engineering >  Translator on python not working, coming with error saying 'BaseBlob.translate() got an unexpec
Translator on python not working, coming with error saying 'BaseBlob.translate() got an unexpec

Time:12-21

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

The code basically asks user to input a number and converts it into the spelling for the number, I need to make a translator for the output. On the GUI it has a combo/dropdown box and the user can select what language it needs to be translated to. However the code does not work. What should I do? Whenever I change the Language an error saying 'BaseBlob.translate() got an unexpected keyword argument' comes up (i made it so a message box shows an error).

CodePudding user response:

According to TextBlob documentation, the arguments for translate are from_lang and to, so your translation line should be:

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