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.

Respuesta :

Answer:

Here is the Python program:

import tkinter  #to create GUI translator application

class Spanish2English:  # class to translate Spanish words to English

 def __init__(self):  # constructor of class

   self.main_window = tkinter.Tk()  # create a main window

   self.top_frame = tkinter.Frame()  # create a frame

   self.middle_frame = tkinter.Frame()#  # create a middle frame

   self.bottom_frame = tkinter.Frame() #create a frame at bottom

   self.top_label = tkinter.Label(self.top_frame,text="Select a spanish word to translate")  #creates a label at top frame with text: Select a Spanish word to translate

   self.top_label.pack()  # to organize the widgets

   self.first_button=tkinter.Button(self.middle_frame, text="uno",command=self.word1)  

#create first button with Spanish word uno and place it in middle frame

   self.second_button = tkinter.Button(self.middle_frame, text="dos",command = self.word2)  

# create second button with Spanish text dos and place it in middle frame

   self.third_button = tkinter.Button(self.middle_frame, text="tres",command = self.word3)  

# create third button with Spanish text  tres and place it in middle frame

   self.first_button.pack(side='top')  #place first button at top

   self.second_button.pack(side='top')  #place second button next

   self.third_button.pack(side='top')  #place third button in end

   self.bottom_left_label = tkinter.Label(self.bottom_frame,text="English translation: ")  #creates label with text English translation:

   self.translation = tkinter.StringVar()  #creates String variable to hold the English translation of the Spanish words

   self.trans_label = tkinter.Label(self.bottom_frame,  textvariable=self.translation)  #creates and places label at the bottom frame which contains the English translation of Spanish words

   self.bottom_left_label.pack(side = 'left')

#organizes the labels

   self.trans_label.pack(side = 'right')

#organizes the frames

   self.top_frame.pack()

   self.middle_frame.pack()

   self.bottom_frame.pack()    

   tkinter.mainloop() #to run the application

 def word1(self): #method to translate word1 i.e. uno

   self.translation.set('one') #set the value of object to one

 def word2(self): #method to translate word2 i.e. dos

   self.translation.set('two') #set the value of object to two

 def word3(self): #method to translate word3 i.e. tres

   self.translation.set('three')  #set the value of object to three

translator = Spanish2English() #run the translator application

Explanation:

The program is well explained in the comments mentioned with each line of program

The program creates a GUI translator application using tkinter standard GUI library. In class Spanish2English first the main window is created which is the root widget. Next this main window is organized into three frames top, middle and bottom. A label is placed in the top frame which displays the text "Select a Spanish word to translate". Next, three buttons are created, for each Spanish word. The first button displays the Spanish word 'uno', second button displays the Spanish word 'dos' and third button displays the Spanish word 'tres'. Next, the two labels are created, one label is to display the text: "English translation: " and the other label is to display the English translation of the Spanish words in each button. In the last, three methods, word1, word2 and word3 are used to set the value of the translation object.

The program along with its output is attached.

Ver imagen mahamnasir
ACCESS MORE
EDU ACCESS
Universidad de Mexico