To clear a Tkinter form, connect the Reset button to a function that checks each widget and clears its value. Use winfo_children() to get the widgets, isinstance() to identify their types, and the appropriate reset method for each type.
def my_reset():
for widget in my_w.winfo_children():
if isinstance(widget, ttk.Combobox):
widget.set("")
elif isinstance(widget, tk.Entry):
widget.delete(0, tk.END)
elif isinstance(widget, tk.Text):
widget.delete("1.0", tk.END)
elif isinstance(widget, tk.Checkbutton):
widget.deselect()
year_var.set("") # Clear the Radiobutton groupPlace this function inside your Tkinter application after creating my_w and the variable year_var. A complete runnable program is provided below.
Different widgets need different methods. A single call to delete() does not work for every Tkinter control.
| Widget | Method to reset | What happens |
|---|---|---|
tk.Entry | delete(0, tk.END) | Clears the single-line input |
ttk.Combobox | set("") | Removes the displayed selection |
tk.Text | delete("1.0", tk.END) | Deletes multiline content |
tk.Checkbutton | deselect() | Unchecks the box |
tk.Radiobutton | variable.set("") | Clears the shared selected value |
winfo_children() returns only widgets directly contained in the specified parent. For controls inside Frames, use the recursive method shown later.
To clear multiple Entry widgets, loop through the parent's child widgets and apply delete(0, tk.END) to each Entry. The same code works whether there are two Entry fields or twenty.
for widget in my_w.winfo_children():
if isinstance(widget, tk.Entry):
widget.delete(0, tk.END)Here, 0 identifies the first character, while tk.END refers to the end of the text.
A Combobox belongs to ttk. Using set("") clears its displayed value, including when the widget was created with state="readonly".
for widget in my_w.winfo_children():
if isinstance(widget, ttk.Combobox):
widget.set("")The dropdown choices remain available; only the current selection is cleared. To select a known default instead, use widget.set("Jan") or another value in its list.
The Text widget uses line-and-character indices. Its content starts at "1.0", not index 0 as in an Entry.
for widget in my_w.winfo_children():
if isinstance(widget, tk.Text):
widget.delete("1.0", tk.END)Using delete(0, "end") on a Text widget can raise a Tcl index error. Always start from "1.0".
The complete program creates two Entry fields, a month Combobox, a multiline Text field, two Checkbuttons, a pair of Radiobuttons, and two buttons. Try entering values and then click Reset: all text fields and selections return to an empty state. The Submit button displays the current values.
Save the program as tkinter_reset.py and run it with a local Python installation that supports Tkinter. Native Tkinter windows require a desktop display, so this example is intended for a computer rather than a hosted notebook.
import tkinter as tk
from tkinter import ttk, messagebox
my_w = tk.Tk()
my_w.title("Tkinter Reset Button - plus2net")
my_w.geometry("500x430")
my_w.columnconfigure(1, weight=1)
# Widgets that accept user input
first_name = tk.Entry(my_w, font=("Arial", 12))
last_name = tk.Entry(my_w, font=("Arial", 12))
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
month_box = ttk.Combobox(my_w, values=months, state="readonly")
notes = tk.Text(my_w, height=3, width=32, bg="lightyellow")
agree = tk.Checkbutton(my_w, text="I Agree")
newsletter = tk.Checkbutton(my_w, text="Newsletter")
year_var = tk.StringVar(value="year2")
year1 = tk.Radiobutton(my_w, text="Year 1", value="year1", variable=year_var)
year2 = tk.Radiobutton(my_w, text="Year 2", value="year2", variable=year_var)
def my_reset():
for widget in my_w.winfo_children():
if isinstance(widget, ttk.Combobox):
widget.set("")
elif isinstance(widget, tk.Entry):
widget.delete(0, tk.END)
elif isinstance(widget, tk.Text):
widget.delete("1.0", tk.END)
elif isinstance(widget, tk.Checkbutton):
widget.deselect()
year_var.set("") # Reset the complete Radiobutton group
def show_values():
messagebox.showinfo("Form values", f"First name: {first_name.get()}\n"
f"Last name: {last_name.get()}\n"
f"Month: {month_box.get()}\n"
f"Year: {year_var.get()}")
# Arrange all widgets directly inside my_w.
tk.Label(my_w, text="First Name").grid(row=0, column=0, padx=10, pady=8, sticky="w")
first_name.grid(row=0, column=1, padx=10, pady=8, sticky="ew")
tk.Label(my_w, text="Last Name").grid(row=1, column=0, padx=10, pady=8, sticky="w")
last_name.grid(row=1, column=1, padx=10, pady=8, sticky="ew")
tk.Label(my_w, text="Month").grid(row=2, column=0, padx=10, pady=8, sticky="w")
month_box.grid(row=2, column=1, padx=10, pady=8, sticky="w")
tk.Label(my_w, text="Notes").grid(row=3, column=0, padx=10, pady=8, sticky="nw")
notes.grid(row=3, column=1, padx=10, pady=8, sticky="ew")
agree.grid(row=4, column=0, padx=10, pady=5)
newsletter.grid(row=4, column=1, padx=10, pady=5)
year1.grid(row=5, column=0, padx=10, pady=5)
year2.grid(row=5, column=1, padx=10, pady=5)
tk.Button(my_w, text="Submit", command=show_values).grid(row=6, column=0, pady=15)
tk.Button(my_w, text="Reset", bg="lightpink", command=my_reset).grid(row=6, column=1, pady=15)
my_w.mainloop()Clicking Reset clears First Name, Last Name, Month, and Notes; unchecks both Checkbuttons; and removes the selection from both Radiobuttons. The remaining labels and buttons stay on screen.
my_w. If your real form groups controls inside Frames or tabs, use a recursive widget traversal.Widgets inside a Frame, LabelFrame, or notebook tab are not direct children of the main window. Use a recursive generator to visit every descendant. This snippet is suitable for use after those widgets have been created.
def all_children(parent):
for child in parent.winfo_children():
yield child
yield from all_children(child)
for widget in all_children(my_w):
if isinstance(widget, ttk.Combobox):
widget.set("")
elif isinstance(widget, (tk.Entry, ttk.Entry)):
widget.delete(0, tk.END)
elif isinstance(widget, tk.Text):
widget.delete("1.0", tk.END)For selection widgets, also reset any associated StringVar, IntVar, or BooleanVar values. The snippet focuses on text-entry controls; you can extend it using the methods in the reference table above.
Sometimes Reset should return the form to its starting values rather than leave all fields empty. After clearing an Entry, insert its default text, set a Combobox option, or restore the initial Radiobutton choice.
# Restore the original default values instead of leaving fields empty.
first_name.delete(0, tk.END)
first_name.insert(0, "Guest")
month_box.set("Jan")
year_var.set("year2")When using themed Checkbuttons, manage their selection through a variable. The checkbox reflects the variable's current Boolean value.
# For ttk.Checkbutton, control the check state with a variable.
selected = tk.BooleanVar(value=True)
check = ttk.Checkbutton(my_w, text="Newsletter", variable=selected)
selected.set(False) # Clear the selectionAn Entry in the disabled state cannot be edited normally. Temporarily enable it, clear the text, and then restore its previous state. Apply this only when clearing a disabled field is intentional.
# Temporarily enable a disabled Entry before clearing its content.
old_state = first_name.cget("state")
first_name.configure(state="normal")
first_name.delete(0, tk.END)
first_name.configure(state=old_state)| Problem | Cause and solution |
|---|---|
| Text widget raises an invalid index error | Use delete("1.0", tk.END) instead of delete(0, tk.END). |
| Some fields do not clear | winfo_children() only returns immediate children. Visit nested Frames recursively. |
| Combobox stays selected | Use set("") to clear its displayed value. For a default, set an allowed selection. |
| Radiobutton selection remains | Reset the variable shared by the Radiobutton group, not each widget individually. |
| Reset button has no effect | Pass the function as command=my_reset. Do not call it during button creation with command=my_reset(). |
| Disabled input cannot be cleared | Temporarily change its state to normal, clear it, and restore the original state. |
| A ttk.Checkbutton is not detected | ttk.Checkbutton is a different class from tk.Checkbutton. Reset its variable, or handle its themed widget state. |
Yes. Loop over winfo_children(), identify each Entry with isinstance(), and call delete(0, tk.END). Use recursion if the widgets are nested.
An Entry starts at character index 0. A Text widget starts at line and character index "1.0".
No. set("") removes the current displayed value while keeping the list of choices.
Store your initial values and assign them again within the reset function, such as year_var.set("year2") or month_box.set("Jan").
The native graphical Tkinter window normally needs a local desktop display. Use a local Python environment for this GUI example.
Continue with Tkinter Button options and commands, Entry widgets, Combobox widgets, Text widgets, Checkbutton selection, and Radiobutton values.
Dynamic Buttons handling » ON / OFF switch using Button images » Python Dictionary tutorial »Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.