|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import messagebox |
| 3 | + |
| 4 | +# Function to display text on label |
| 5 | +def display_text(): |
| 6 | + text = input_text.get() |
| 7 | + if text: |
| 8 | + label_text.config(text="You entered: " + text) |
| 9 | + else: |
| 10 | + messagebox.showerror("Error", "Please enter some text.") |
| 11 | + |
| 12 | +# Function to clear the input field |
| 13 | +def clear_input(): |
| 14 | + input_text.delete(0, tk.END) |
| 15 | + label_text.config(text="") |
| 16 | + |
| 17 | +# Initialize the root window |
| 18 | +root = tk.Tk() |
| 19 | +root.title("Tkinter Example Application") |
| 20 | + |
| 21 | +# Create a label for input instruction |
| 22 | +label_input = tk.Label(root, text="Enter text:") |
| 23 | +label_input.pack() |
| 24 | + |
| 25 | +# Create an entry for text input |
| 26 | +input_text = tk.Entry(root) |
| 27 | +input_text.pack() |
| 28 | + |
| 29 | +# Create a frame for buttons |
| 30 | +button_frame = tk.Frame(root) |
| 31 | +button_frame.pack() |
| 32 | + |
| 33 | +# Create a button to display text |
| 34 | +button_display = tk.Button(button_frame, text="Display", command=display_text) |
| 35 | +button_display.pack(side=tk.LEFT, padx=5) |
| 36 | + |
| 37 | +# Create a button to clear input |
| 38 | +button_clear = tk.Button(button_frame, text="Clear", command=clear_input) |
| 39 | +button_clear.pack(side=tk.LEFT, padx=5) |
| 40 | + |
| 41 | +# Create a label to display the input text |
| 42 | +label_text = tk.Label(root, text="") |
| 43 | +label_text.pack() |
| 44 | + |
| 45 | +# Run the main loop |
| 46 | +root.mainloop() |
0 commit comments