from tkinter import *  # Import everything from tkinter to build the GUI window

# Create the main application window
root = Tk()
root.title("Tkinter Button Lesson")  # Set the window title
root.geometry("400x300")  # Set a fixed window size (width x height)

# ─────────────────────────────────────────
# FUNCTIONS
# Always define functions before using them
# ─────────────────────────────────────────


def clickMe():
    # Read what the user typed using .get()
    name = inputField.get()

    # Validate: check if the input field is empty
    if name == "":
        # If empty, update the existing label to show a reminder message
        myLabel.config(text="Please enter your name first!")
    else:
        # If the user typed something, create a new label showing the welcome message
        welcomeLabel = Label(root, text="Welcome to Tkinter Tutor " + name)
        welcomeLabel.pack()


def clearAll():
    # Delete everything inside the input field from position 0 to the END
    inputField.delete(0, END)
    # Reset the top label back to its original message
    myLabel.config(text="Hello, Programmer")


# ─────────────────────────────────────────
# WIDGETS
# Widgets are the elements you see on screen
# such as labels, buttons, and input fields
# ─────────────────────────────────────────

# Entry widget — a text field where the user types their name
# width=50 controls how wide the field appears
# borderwidth=5 adds a small border around the field
# bg and fg control background and text color
inputField = Entry(root, width=50, borderwidth=5, bg="white", fg="black")
inputField.pack()  # Add the input field to the window

# Label widget — displays a static or updatable message
# We use .config() later to update this label's text dynamically
myLabel = Label(root, text="Hello, Programmer")
myLabel.pack()

# Button widget — clicking this calls the clickMe() function
# command=clickMe connects the button click to our function
# fg and bg control the button text and background colors
myButton = Button(
    root, text="Enter your name!", fg="red", bg="white", command=clickMe, borderwidth=0
)
myButton.pack()

# Clear button — clicking this calls the clearAll() function
# This resets the input field and the label back to default
clearButton = Button(
    root, text="Clear", fg="black", bg="lightgray", command=clearAll, borderwidth=0
)
clearButton.pack()

# ─────────────────────────────────────────
# MAIN LOOP
# This keeps the window open and listening
# for user actions like clicks and typing.
# Nothing below this line will ever run.
# ─────────────────────────────────────────
root.mainloop()
