Skip to content
Laviyx Learn

Try

Python Lab · Experiment 38

GUI form with two labels, two text fields, Submit and Reset buttons

PythonWeek 8JNTUH R22 · CS206ES

Aim

To write a Python GUI program using tkinter that creates a window with two text labels, two text fields and two buttons, Submit and Reset, where Submit displays the entered values and Reset clears the fields.

Algorithm

  1. Create the main window with tk.Tk() and give it a title.
  2. Create two labels, "Name" and "Email", and two Entry text fields, and place them in a grid: labels in column 0, fields in column 1.
  3. Write the function submit(): read both fields with get(). If either is empty, show a message asking to fill both; otherwise show the entered name and email in a result label.
  4. Write the function reset(): clear both fields with delete(0, tk.END), clear the result label, and put the cursor back in the Name field.
  5. Create the Submit and Reset buttons, with command=submit and command=reset.
  6. Add the result label below the buttons.
  7. Call mainloop() to show the window and wait for the user's actions.

Program

Python
import tkinter as tk


def submit():
    name = name_entry.get().strip()
    email = email_entry.get().strip()
    if name == "" or email == "":
        result_label.config(text="Please fill in both fields", fg="red")
    else:
        result_label.config(text=f"Submitted\nName: {name}\nEmail: {email}", fg="green")


def reset():
    name_entry.delete(0, tk.END)
    email_entry.delete(0, tk.END)
    result_label.config(text="")
    name_entry.focus_set()


root = tk.Tk()
root.title("Registration Wizard")

name_label = tk.Label(root, text="Name")
name_label.grid(row=0, column=0, padx=10, pady=8, sticky="w")
name_entry = tk.Entry(root, width=30)
name_entry.grid(row=0, column=1, padx=10, pady=8)

email_label = tk.Label(root, text="Email")
email_label.grid(row=1, column=0, padx=10, pady=8, sticky="w")
email_entry = tk.Entry(root, width=30)
email_entry.grid(row=1, column=1, padx=10, pady=8)

submit_button = tk.Button(root, text="Submit", width=10, command=submit)
submit_button.grid(row=2, column=0, padx=10, pady=10)
reset_button = tk.Button(root, text="Reset", width=10, command=reset)
reset_button.grid(row=2, column=1, padx=10, pady=10, sticky="w")

result_label = tk.Label(root, text="", justify="left")
result_label.grid(row=3, column=0, columnspan=2, padx=10, pady=8, sticky="w")

name_entry.focus_set()
root.mainloop()

Output

The program opens a small window titled "Registration Wizard". It contains:

  • Row 1: the label "Name" and an empty text field next to it. The cursor starts in this field.
  • Row 2: the label "Email" and an empty text field next to it.
  • Row 3: two buttons, "Submit" and "Reset".
  • Below the buttons, an empty area for the result.

When you type a name and an email and click Submit, three green lines appear below the buttons: "Submitted", then "Name: " followed by the name you typed, then "Email: " followed by the email. If either field is empty when you click Submit, the red message "Please fill in both fields" appears instead.

Clicking Reset empties both text fields, removes the message below the buttons, and puts the cursor back in the Name field. Nothing is printed in the terminal.

How it works

Label shows text, Entry is a one-line text field and Button runs the function given as command when it is clicked. Pass the function name without brackets, command=submit: writing command=submit() would call it once while the window is being built, and the button would then do nothing.

grid(row=..., column=...) places each widget in a table of rows and columns. padx and pady add space around it, and sticky="w" lines it up on the left (west) side of its cell.

entry.get() returns the text in a field, and entry.delete(0, tk.END) removes everything from the first character to the end. label.config(text=...) changes the text of a label after it has been created.

The result is shown in a label inside the window. To show it in a pop-up box instead, use from tkinter import messagebox and messagebox.showinfo("Submitted", f"Name: {name}\nEmail: {email}").

Common mistakes

  • Writing command=submit() with brackets. The function runs once when the button is created, not when it is clicked. Use command=submit.

  • Mixing pack() and grid() for widgets in the same window. tkinter cannot use both managers in one container and raises a TclError. Use one of them.

  • Calling .grid() on the same line as creating the widget, as in e = tk.Entry(root).grid(...). grid returns None, so e becomes None and e.get() fails. Create the widget first, then call grid on a separate line.

  • Forgetting root.mainloop(). The window does not stay open and the buttons never respond.

Viva questions

Try to answer each one aloud before you open it.

  • What is tkinter?

    tkinter is Python's standard library module for building graphical user interfaces. It provides widgets such as windows, labels, text fields, buttons and canvases.

  • What is the difference between a Label and an Entry widget?

    A Label only displays text or an image. An Entry is a one-line field where the user can type text, which the program reads with get().

  • How does a button run a function when clicked?

    The function is passed as the button's command option, without brackets. tkinter calls it each time the button is clicked.

  • What are the three geometry managers in tkinter?

    pack, which stacks widgets one after another; grid, which places them in rows and columns; and place, which puts them at exact positions.

  • How do you clear the text in an Entry widget?

    With entry.delete(0, tk.END), which deletes from the first character to the end.

  • What is event-driven programming?

    A style where the program waits for events such as clicks and key presses and runs a function when each one happens. mainloop() is the loop that waits for these events in tkinter.

Learn the concepts

All Python Programming Lab experiments →