Answered step by step
Verified Expert Solution
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 pythontranslator import Translator # not a native library requires
# "pip install pythontranslator"
# pip install requests
# pip install bs
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 langTranslatefromLtoL,msg:
translator Translator
translation translator.translatemsgtoL,fromL
return translation
tryAgain y
while tryAgain y or tryAgain s:
if tryAgain y:
fromLanguage inputEnter language translating from:
while fromLanguage.title not in VALIDLANGUAGES:
print Invalid From language try again"
fromLanguage inputEnter language translating from:
toLanguage inputEnter language translating to:
while toLanguage.title not in VALIDLANGUAGES:
print Invalid To language try again"
toLanguage inputEnter language translating to:
originalText inputEnter the statement you want translated:
translation langTranslatefromLanguagetoLanguage,originalText
print ft from fromLanguage to toLanguage is: translation
tryAgain inputEnter 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 initself:
# Create the main window.
self.mainwindow tkinter.Tk
# Create a Listbox widget.
self.languagelistbox tkinter.Listbox
self.mainwindow, width height
self.languagelistbox.packpadx pady
# Populate the Listbox with the list contents of language list.
for language in VALIDLANGUAGES:
self.languagelistbox.inserttkinterEND, language
# Create a button to get the selected item.
self.getbutton tkinter.Button
self.mainwindow, text'Get Item',
commandself.retrievelanguage
self.getbutton.packpadx pady
# Start the main loop.
tkinter.mainloop
def retrievelanguageself:
# Get the index of the selected item.
indexes self.languagelistbox.curselection
# If an item is selected, display it
if lenindexes:
tkinter.messagebox.showinfo
messageself.languagelistbox.getindexes
else:
tkinter.messagebox.showinfo
messageNo item selected.
# Create an instance of the ListBoxSelection class.
if namemain:
listboxselection ListBoxSelection
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started