Leandro
Leandro

Reputation: 47

How can I make a translator with python?

I started literally a couple of days ago, and I need help :( I want to create a program to translate a phrase to something else

Ex:

Program

000000000000000000000000000000000000000000

"Phrase to translate"

(Here the person writes the sentence)

A button to activate the command

(And here the translated phrase appears)

000000000000000000000000000000000000000000

Or something like that

I already have the code to change the words, and I am creating the interface, and I have already learned to create windows but I still don't know how to paste my translation :(

This is what I have

frase=input("Escribe la frase: ")

entrada="abcdefghilmnopqrstuvxyz"
salida="mnopqrstuvxyzabcdefghil"
letras=frase.maketrans(entrada,salida)

print(frase.translate(letras))
import tkinter as tk


ventana=tk.Tk()
ventana.title("traductor")
ventana.geometry('200x300')
ventana.configure(background='white')

frase=tk.Entry(ventana)
frase.pack(fill=tk.X,padx=5,pady=5,ipadx=5,ipady=5)

root = tk.Button(ventana,text="Traducir:",bg="black",fg="white")
root.pack(padx=20,pady=10)

How can I "combine" these two codes (?

Upvotes: 0

Views: 121

Answers (2)

user15801675
user15801675

Reputation:

Please try this piece of code.

import tkinter as tk
def translate():
    #==== Forget the position of the widgets in the window
    for widget in ventana.winfo_children():
        widget.pack_forget()
    #=== Again place the entry box and the button
    frase.pack(fill=tk.X,padx=5,pady=5,ipadx=5,ipady=5)
    root.pack(padx=20,pady=10)
    #==== Provide the text entered in the entry field
    entrada="abcdefghilmnopqrstuvxyz"
    salida="mnopqrstuvxyzabcdefghil"
    letras=org_phrase.get().maketrans(entrada,salida)
    #===print the translation (optional)
    print(org_phrase.get().translate(letras))
    #=== set it as label to display it
    tk.Label(ventana,bg="white",text=org_phrase.get().translate(letras)).pack()
ventana=tk.Tk()
ventana.title("traductor")
ventana.geometry('200x300')
ventana.configure(background='white')
#=== Using stringvar to get the value
org_phrase=tk.StringVar()
#trans_phrase=tk.StringVar()
frase=tk.Entry(ventana,textvariable=org_phrase)
frase.pack(fill=tk.X,padx=5,pady=5,ipadx=5,ipady=5)

root =tk.Button(ventana,text="Traducir:",bg="black",fg="white",command=translate)
root.pack(padx=20,pady=10)
root.mainloop()

Upvotes: 0

Thalis
Thalis

Reputation: 176

What you need to do is create a tk widget called label:

label = tk.Label(ventana, width=20)
label.pack()

Then you can simply make a function called translate and configure the text of the label however you want

def translate(phrase):
    label.config(text=phrase)

lastly you should bind the function translate to the button so that when you press it the magic happens:)
Like this:

root = tk.Button(ventana,text="Traducir:",bg="black",fg="white", command=lambda: translate(phrase=frase.get()))

This is some ready to try code based on your question

import tkinter as tk


ventana=tk.Tk()
ventana.title("traductor")
ventana.geometry('200x300')
ventana.configure(background='white')

frase=tk.Entry(ventana)
frase.pack(fill=tk.X,padx=5,pady=5,ipadx=5,ipady=5)

label = tk.Label(ventana, width=20)
label.pack()

dic = {'Hello':'Bonjour'}
def translate(phrase):
    if phrase in dic.keys():
        text = dic[phrase]
    else:
        text='Cannot translate text'
        
    label.config(text=text)
    
root = tk.Button(ventana,text="Traducir:",bg="black",fg="white", command=lambda: translate(phrase=frase.get()))
root.pack(padx=20,pady=10)

ventana.mainloop()

For the translation part the best you can do is create a dictionary. This way when someone enters a specific word in your EntryBox the program will be able to translate it: You can do so like this:

dic = {'Good Morning':'Bonjour', 'Yes':'Oui'}
def translate(phrase):
    if phrase in dic.keys():
        text = dic[phrase]
    else:
        text='Cannot translate text'
        
    label.config(text=text)

Last but not least, it's usually good to add a mainloop() call in the end of your program so as for the events happening in your app to be handled correctly. In your case you should add ventana.mainloop()

Upvotes: 1

Related Questions