In: Computer Science
Using Python
Look at the following list of Spanish words and their meanings.
Spanish English
uno one
dos two
tres three
Write a GUI program that translates the Spanish words to English. The window should have three buttons, one for each Spanish word. When the user clicks a button, the program displays the English translation in a label.
Python code for the given problem statement is as follows:
(Appropriate Comments are given as and when necessary for your easy understanding)
from tkinter import *
def clear_all() :
translate_field.delete(0, END)
# set focus on the translate_field entry box
translate_field.focus_set()
def translate1():
translate_field.insert(10,"One")
def translate2():
translate_field.insert(10,"Two")
def translate3():
translate_field.insert(10,"Three")
root = Tk()
# Set the background colour of GUI window
root.configure(background = 'light green')
# Set the configuration of GUI window
root.geometry("400x250")
# set the name of tkinter GUI window
root.title("Translate")
# Create a English Translation : label
label1 = Label(root, text = "English Translation : ",fg = 'black', bg = 'red')
# grid method is used for placing
# the widgets at respective positions
# in table like structure .
# padx keyword argument used to set paading along x-axis .
# pady keyword argument used to set paading along y-axis .
label1.grid(row = 1, column = 0, padx = 10, pady = 10)
# Create a text box
# for output of translated information
translate_field = Entry(root)
translate_field.grid(row = 1, column = 1, padx = 10, pady = 10)
button1 = Button(root, text = "Uno", bg = "red",fg = "black", command = translate1)
button2 = Button(root, text = "Dos", bg = "red",fg = "black", command = translate2)
button3 = Button(root, text = "Tres", bg = "red",fg = "black", command = translate3)
# Create a Clear Button and attached
# to clear_all function
button4 = Button(root, text = "Clear", bg = "cyan",fg = "black", command = clear_all)
button1.grid(row = 4, column = 1, pady = 10)
button2.grid(row = 6, column = 1, pady = 10)
button3.grid(row = 8, column = 1, pady = 10)
button4.grid(row = 10, column = 1, pady = 10)
# Start the GUI
root.mainloop()
Output:
I hope you find the solution helpful and insightful.
Keep Learning !