Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- # Create a window object
- window = tk.Tk()
- # Set the window title
- window.title("Template Viewer")
- # Set the window size
- window.geometry("600x400")
- # Create a label widget to display the input text
- output_label = tk.Label(window, text="", justify="left", wraplength=500)
- # Create a text input widget that spans most of the window
- input_field = tk.Text(window, height=20, width=70)
- # Define a function to handle button click event
- import random
- import re
- def show_input():
- input_text = input_field.get("1.0", "end-1c")
- words = re.findall(r"[\w']+|[^_\s]*__[^_\s]*", input_text)
- while any(word.startswith("__") and word.endswith("__") for word in words):
- for i in range(len(words)):
- if words[i].startswith("__") and words[i].endswith("__"):
- word = words[i][2:-2]
- try:
- with open(f"{word}.txt", "r") as f:
- lines = f.readlines()
- random_line = random.choice(lines)
- words[i] = random_line.strip()
- except FileNotFoundError:
- pass
- output_text = " ".join(words)
- output_label.config(text=output_text, fg="black")
- # Create a button widget to display the input text
- show_button = tk.Button(window, text="Generate", command=show_input, bg="white", fg="black")
- # Define a function to handle copy button click event
- def copy_output():
- output_text = output_label.cget("text")
- window.clipboard_clear()
- window.clipboard_append(output_text)
- # Create a button widget to copy the output text
- copy_button = tk.Button(window, text="Copy Output", command=copy_output, bg="yellow")
- # Setthe positions of the widgets using the grid layout manager
- output_label.grid(row=0, column=1, columnspan=2, padx=10, pady=10, sticky="nsew")
- input_field.grid(row=1, column=0, padx=10, pady=10, sticky="nsew")
- show_button.grid(row=0, padx=10, pady=10, sticky="w")
- copy_button.grid(row=0, column=3, padx=10, pady=10, sticky="e")
- # Configure the grid layout manager to expand rows and columns
- window.grid_columnconfigure(0, weight=1)
- window.grid_rowconfigure(0, weight=1)
- window.grid_rowconfigure(1, weight=1)
- # Start the window event loop
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement