Overview
CTkEntry is a text input widget with modern styling, featuring rounded corners, customizable borders, placeholder text, and textvariable support. It wraps tkinter’s Entry widget with enhanced visual styling.
Constructor
CTkEntry(
master,
width=140,
height=28,
corner_radius=None,
border_width=None,
bg_color="transparent",
fg_color=None,
border_color=None,
text_color=None,
placeholder_text_color=None,
textvariable=None,
placeholder_text=None,
font=None,
state=tkinter.NORMAL,
**kwargs
)
Parameters
Width of the entry widget in pixels
Height of the entry widget in pixels
corner_radius
Optional[int]
default:"None"
Corner radius in pixels. If None, uses theme default
border_width
Optional[int]
default:"None"
Border width in pixels. If None, uses theme default
bg_color
Union[str, Tuple[str, str]]
default:"'transparent'"
Background color behind the widget. Can be a single color or tuple of (light_mode, dark_mode) colors
fg_color
Optional[Union[str, Tuple[str, str]]]
default:"None"
Foreground color of the entry field. If None, uses theme default
border_color
Optional[Union[str, Tuple[str, str]]]
default:"None"
Border color. If None, uses theme default
text_color
Optional[Union[str, Tuple[str, str]]]
default:"None"
Color of the text entered by the user. If None, uses theme default
placeholder_text_color
Optional[Union[str, Tuple[str, str]]]
default:"None"
Color of the placeholder text. If None, uses theme default
textvariable
Union[tkinter.Variable, None]
default:"None"
Tkinter variable (StringVar) to link with the entry’s content
placeholder_text
Union[str, None]
default:"None"
Text displayed when the entry is empty and not focused
font
Optional[Union[tuple, CTkFont]]
default:"None"
Font for the text. Can be a tuple like (“Arial”, 12) or a CTkFont object
state
str
default:"tkinter.NORMAL"
State of the widget: tkinter.NORMAL, tkinter.DISABLED, or “readonly”
Additional Tkinter Entry Parameters
CTkEntry also accepts these standard tkinter.Entry parameters:
exportselection
insertborderwidth
insertofftime
insertontime
insertwidth
justify
selectborderwidth
show (for password fields)
takefocus
validate
validatecommand
xscrollcommand
Methods
get()
Returns the current text in the entry. Returns empty string if placeholder is active.
Returns: str - The text content of the entry
insert()
Inserts text at the specified position.
entry.insert(index, string)
Parameters:
index (int): Position to insert text (0 for beginning, tkinter.END for end)
string (str): Text to insert
delete()
Deletes text from the entry.
entry.delete(first_index, last_index=None)
Parameters:
first_index (int): Starting position to delete from
last_index (int, optional): Ending position. If None, deletes only one character
Configures widget parameters.
entry.configure(fg_color="blue", border_width=3)
Common configurable parameters: width, height, corner_radius, border_width, fg_color, border_color, text_color, placeholder_text_color, placeholder_text, font, state, textvariable
cget()
Returns the value of a configuration parameter.
current_color = entry.cget("fg_color")
Parameters:
attribute_name (str): Name of the parameter to retrieve
Returns: Value of the specified parameter
Usage Examples
Basic Entry
import customtkinter as ctk
root = ctk.CTk()
entry = ctk.CTkEntry(root, placeholder_text="Enter your name")
entry.pack(pady=20, padx=20)
root.mainloop()
Getting and Setting Values
# Create entry with initial value
entry = ctk.CTkEntry(root)
entry.insert(0, "Initial text")
# Get current value
def submit():
text = entry.get()
print(f"User entered: {text}")
# Clear entry
def clear():
entry.delete(0, ctk.END)
submit_btn = ctk.CTkButton(root, text="Submit", command=submit)
submit_btn.pack()
Password Entry
password_entry = ctk.CTkEntry(
root,
placeholder_text="Enter password",
show="*"
)
password_entry.pack(pady=20)
Using with Textvariable
import tkinter as tk
text_var = tk.StringVar()
entry = ctk.CTkEntry(
root,
textvariable=text_var,
placeholder_text="Linked to variable"
)
entry.pack(pady=20)
# Access value through variable
def get_value():
print(text_var.get())
# Set value through variable
text_var.set("New value")
Styled Entry
entry = ctk.CTkEntry(
root,
width=300,
height=40,
corner_radius=20,
border_width=2,
border_color="#3b8ed0",
fg_color="#1f6aa5",
text_color="white",
placeholder_text="Styled input",
placeholder_text_color="#90c0df",
font=("Helvetica", 14)
)
entry.pack(pady=20, padx=20)
Readonly Entry
entry = ctk.CTkEntry(root, state="readonly")
entry.insert(0, "This cannot be edited")
entry.pack(pady=20)
Entry with Validation
def validate_number(value):
if value == "":
return True
try:
float(value)
return True
except ValueError:
return False
vcmd = (root.register(validate_number), '%P')
entry = ctk.CTkEntry(
root,
placeholder_text="Enter a number",
validate="key",
validatecommand=vcmd
)
entry.pack(pady=20)