Guest User

Untitled

a guest
Oct 7th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3. # Defining the functions to translate individual words to pig latin
  4. def pig_latin_word(word):
  5. vowels = ['a', 'e', 'i', 'o', 'u'] # Listing what letters constitute vowels
  6. if word[0] in vowels:
  7. return word + "way"
  8. else:
  9. cons = 0
  10. while cons < len(word) and word[cons] not in vowels:
  11. cons += 1
  12. return word[cons:] + word[:cons] + "ay"
  13.  
  14.  
  15. # Defining the function of turning a whole sentence into pig latin
  16. def pig_latin_sentence(sentence):
  17. words = sentence.split()
  18. translated_words = [pig_latin_word(word) for word in words]
  19. translated_sentence = " ".join(translated_words)
  20. return translated_sentence
  21.  
  22. # Define a function to translate the sentence and update the result label
  23. def translate():
  24. original_text = input_field.get()
  25. pig_latin = pig_latin_sentence(original_text)
  26. result_label.config(text="Pig Latin: " + pig_latin)
  27.  
  28. # Create the main tkinter window
  29. root = tk.Tk()
  30. root.title("Pig Latin Translator")
  31.  
  32. # Create and pack GUI elements
  33. input_label = tk.Label(root, text="Enter sentence here:")
  34. input_label.pack()
  35.  
  36. input_field = tk.Entry(root, width=40)
  37. input_field.pack()
  38.  
  39. translate_button = tk.Button(root, text="Translate", command=translate)
  40. translate_button.pack()
  41. close_button = tk.Button(root, text="close", command=root.destroy)
  42. close_button.pack()
  43.  
  44. result_label = tk.Label(root, text="")
  45. result_label.pack()
  46.  
  47.  
  48. # Final usage for code
  49. original = input("Enter sentence here ")
  50. pig_latin = pig_latin_sentence(original)
  51.  
  52. # Start the tkinter main loop
  53. root.mainloop()
  54.  
  55. # Write the result to a text file
  56. with open("pig_latin_output.txt", "w") as file:
  57. file.write(pig_latin.lower())
  58.  
  59. print('Result written to \'pig_latin_output.txt\'')
Advertisement
Add Comment
Please, Sign In to add comment