Advertisement
Guest User

Untitled

a guest
Jun 1st, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3. # Create a window object
  4. window = tk.Tk()
  5.  
  6. # Set the window title
  7. window.title("Template Viewer")
  8.  
  9. # Set the window size
  10. window.geometry("600x400")
  11.  
  12. # Create a label widget to display the input text
  13. output_label = tk.Label(window, text="", justify="left", wraplength=500)
  14.  
  15. # Create a text input widget that spans most of the window
  16. input_field = tk.Text(window, height=20, width=70)
  17.  
  18. # Define a function to handle button click event
  19. import random
  20. import re
  21.  
  22. def show_input():
  23. input_text = input_field.get("1.0", "end-1c")
  24. words = re.findall(r"[\w']+|[^_\s]*__[^_\s]*", input_text)
  25. while any(word.startswith("__") and word.endswith("__") for word in words):
  26. for i in range(len(words)):
  27. if words[i].startswith("__") and words[i].endswith("__"):
  28. word = words[i][2:-2]
  29. try:
  30. with open(f"{word}.txt", "r") as f:
  31. lines = f.readlines()
  32. random_line = random.choice(lines)
  33. words[i] = random_line.strip()
  34. except FileNotFoundError:
  35. pass
  36. output_text = " ".join(words)
  37. output_label.config(text=output_text, fg="black")
  38.  
  39. # Create a button widget to display the input text
  40. show_button = tk.Button(window, text="Generate", command=show_input, bg="white", fg="black")
  41.  
  42. # Define a function to handle copy button click event
  43. def copy_output():
  44. output_text = output_label.cget("text")
  45. window.clipboard_clear()
  46. window.clipboard_append(output_text)
  47.  
  48. # Create a button widget to copy the output text
  49. copy_button = tk.Button(window, text="Copy Output", command=copy_output, bg="yellow")
  50.  
  51. # Setthe positions of the widgets using the grid layout manager
  52. output_label.grid(row=0, column=1, columnspan=2, padx=10, pady=10, sticky="nsew")
  53. input_field.grid(row=1, column=0, padx=10, pady=10, sticky="nsew")
  54. show_button.grid(row=0, padx=10, pady=10, sticky="w")
  55. copy_button.grid(row=0, column=3, padx=10, pady=10, sticky="e")
  56.  
  57. # Configure the grid layout manager to expand rows and columns
  58. window.grid_columnconfigure(0, weight=1)
  59. window.grid_rowconfigure(0, weight=1)
  60. window.grid_rowconfigure(1, weight=1)
  61.  
  62. # Start the window event loop
  63. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement