Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- # Defining the functions to translate individual words to pig latin
- def pig_latin_word(word):
- vowels = ['a', 'e', 'i', 'o', 'u'] # Listing what letters constitute vowels
- if word[0] in vowels:
- return word + "way"
- else:
- cons = 0
- while cons < len(word) and word[cons] not in vowels:
- cons += 1
- return word[cons:] + word[:cons] + "ay"
- # Defining the function of turning a whole sentence into pig latin
- def pig_latin_sentence(sentence):
- words = sentence.split()
- translated_words = [pig_latin_word(word) for word in words]
- translated_sentence = " ".join(translated_words)
- return translated_sentence
- # Define a function to translate the sentence and update the result label
- def translate():
- original_text = input_field.get()
- pig_latin = pig_latin_sentence(original_text)
- result_label.config(text="Pig Latin: " + pig_latin)
- # Create the main tkinter window
- root = tk.Tk()
- root.title("Pig Latin Translator")
- # Create and pack GUI elements
- input_label = tk.Label(root, text="Enter sentence here:")
- input_label.pack()
- input_field = tk.Entry(root, width=40)
- input_field.pack()
- translate_button = tk.Button(root, text="Translate", command=translate)
- translate_button.pack()
- close_button = tk.Button(root, text="close", command=root.destroy)
- close_button.pack()
- result_label = tk.Label(root, text="")
- result_label.pack()
- # Final usage for code
- original = input("Enter sentence here ")
- pig_latin = pig_latin_sentence(original)
- # Start the tkinter main loop
- root.mainloop()
- # Write the result to a text file
- with open("pig_latin_output.txt", "w") as file:
- file.write(pig_latin.lower())
- print('Result written to \'pig_latin_output.txt\'')
Advertisement
Add Comment
Please, Sign In to add comment