Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python: How do I fix my code to output a GUI, to select a language and translate from english to another selected language? My code

Python: How do I fix my code to output a GUI, to select a language and translate from english to another selected language?
My code so far:
from python_translator import Translator # not a native library requires
# "pip install python_translator"
# pip install requests
# pip install bs4
VALIDLANGUAGES =['German','Polish','Spanish','English','Dutch', 'Turkish',
'French','Italian','Slovak','Romanian','Swahili','Finnish',
'Afrikaans','Greek','Indonesian','Lithuanian','Catalan',
'Tagalog','Portuguese','Croatian','Russian','Norwegian',
'Danish','Slovenian','Welsh','Albanian','Korean','Somali',
'Czech','Estonian','Swedish','Hungarian','Latvian',
'Vietnamese','Japanese','Arabic','Thai','Bulgarian']
def langTranslate(fromL,toL,msg):
translator = Translator()
translation = translator.translate(msg,toL,fromL)
return translation
tryAgain ="y"
while tryAgain =="y" or tryAgain =="s":
if tryAgain =="y":
fromLanguage = input("Enter language translating from: ")
while fromLanguage.title() not in VALIDLANGUAGES:
print ("Invalid From language - try again")
fromLanguage = input("Enter language translating from: ")
toLanguage = input("Enter language translating to: ")
while toLanguage.title() not in VALIDLANGUAGES:
print ("Invalid To language - try again")
toLanguage = input("Enter language translating to: ")
originalText = input("Enter the statement you want translated: ")
translation = langTranslate(fromLanguage,toLanguage,originalText)
print (f"\t from {fromLanguage} to {toLanguage} is: {translation}")
tryAgain = input("Enter 'n' to end program
"+\
"or enter 's' to try again with same from and to language
"+\
"or 'y'to try again with new from and to languages
"+
"-->")
Language List box if necessary:
import tkinter
import tkinter.messagebox
VALIDLANGUAGES =['German','Polish','Spanish','English','Dutch', 'Turkish',
'French','Italian','Slovak','Romanian','Swahili','Finnish',
'Afrikaans','Greek','Indonesian','Lithuanian','Catalan',
'Tagalog','Portuguese','Croatian','Russian','Norwegian',
'Danish','Slovenian','Welsh','Albanian','Korean','Somali',
'Czech','Estonian','Swedish','Hungarian','Latvian',
'Vietnamese','Japanese','Arabic','Thai','Bulgarian']
class ListBoxSelection:
def __init__(self):
# Create the main window.
self.main_window = tkinter.Tk()
# Create a Listbox widget.
self.language_listbox = tkinter.Listbox(
self.main_window, width=0, height=0)
self.language_listbox.pack(padx=10, pady=5)
# Populate the Listbox with the list contents of language list.
for language in VALIDLANGUAGES:
self.language_listbox.insert(tkinter.END, language)
# Create a button to get the selected item.
self.get_button = tkinter.Button(
self.main_window, text='Get Item',
command=self.__retrieve_language)
self.get_button.pack(padx=10, pady=5)
# Start the main loop.
tkinter.mainloop()
def __retrieve_language(self):
# Get the index of the selected item.
indexes = self.language_listbox.curselection()
# If an item is selected, display it.
if (len(indexes)>0):
tkinter.messagebox.showinfo(
message=self.language_listbox.get(indexes[0]))
else:
tkinter.messagebox.showinfo(
message='No item selected.')
# Create an instance of the ListBoxSelection class.
if __name__=='__main__':
listbox_selection = ListBoxSelection()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How do exemptions affect the amount of tax you will pay?

Answered: 1 week ago

Question

Tell me about yourself.

Answered: 1 week ago

Question

What is one of the skills required for independent learning?Explain

Answered: 1 week ago