Tkinter Entry - Set Default Value
Tkinter - Set default value for Entry widget
In Tkinter, you can set a default value in an Entry widget programmatically using Entry.insert() method.
To set a default value in the Entry field in Tkinter, call the insert() method and specify 0 index and default value as arguments.
entry.insert(0, default_value)
where
- entry is the Entry widget object in which we would like to set default value.
- default_value is the initial value which is filled in the Entry widget
In this tutorial, you will learn how to set a default value for an Entry widget in Tkinter, with examples.
Examples
1. Set default value of "White" in the Entry widget
In this example, we create a basic Tk window with an Entry widget. We would like to read the favorite color of the user. We set White as default value for this Entry widget.
Python Program
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("PythonExamples.org")
window.geometry("300x200")
label = tk.Label(window, text="Enter your favorite color")
label.pack()
# Create an Entry widget with default text
default_text = "White"
entry = tk.Entry(window)
entry.insert(0, default_text)
entry.pack()
# Run the application
window.mainloop()
Output
In Windows
In MacOS
Run the program.
Summary
In this Python Tkinter tutorial, we learned how to set a default value in the Entry widget in Tkinter, with examples.