CustomTkinter provides the CTkEntry widget, allowing you to create modern and customizable input fields. In this tutorial, we will learn how to create and personalize an Entry widget in CustomTkinter.
Creating a Simple Entry in CustomTkinter
Here is a basic example of how to create a simple Entry:
import customtkinter as ctk
# Main class
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.geometry("400x200")
self.title("Creating Entry")
# Creating a simple Entry
self.entry = ctk.CTkEntry(self, placeholder_text="Type something here")
self.entry.place(relx=0.5, rely=0.5, anchor="center") # Centering the entry
# Initializing the App
app = App()
app.mainloop()
Description:
-
The text entry is created using the CTkEntry widget, and the placeholder text (a hint for users) is set using the placeholder_text argument.
-
The place() method positions the Entry in the center of the window using relx=0.5, rely=0.5, and anchor="center" for alignment.
Customizing an Entry in CustomTkinter
You can customize the appearance and behavior of an Entry in several ways, such as changing colors, width, height, and font. Here is an example of a customized Entry:
import customtkinter as ctk
# Main class
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.geometry("400x200")
self.title("Customized Entry")
# Creating a customized Entry
self.entry = ctk.CTkEntry(
self,
placeholder_text="Enter your name",
width=250, # Entry width
height=40, # Entry height
fg_color=("white", "gray75"), # Background color
text_color=("black", "white"), # Text color
corner_radius=10, # Rounded corners
font=("Arial", 14) # Custom font
)
self.entry.place(relx=0.5, rely=0.5, anchor="center") # Centering the entry
# Initializing the App
app = App()
app.mainloop()
Customization Features in the Example:
-
fg_color: Defines the background color of the Entry.
-
text_color: Defines the text color inside the Entry.
-
corner_radius: Adds rounded borders to the Entry.
-
font: Allows setting the text font inside the Entry.
CTkEntry Arguments
Here are some key arguments you can use when creating an Entry with CTkEntry:
Argument | Description |
---|---|
master | Defines the parent widget (window or frame). |
placeholder_text | Text that appears as a hint to the user (placeholder). |
width | Width of the Entry (in pixels). |
height | Height of the Entry (in pixels). |
fg_color | Background color of the Entry (can be a color tuple for light and dark modes). |
text_color | Color of the text inside the Entry. |
corner_radius | Radius of the Entry's corners (for rounded edges). |
font | Font of the Entry text (name, size). |
show | Characters to be displayed, useful for passwords. |
state | Defines the Entry state (normal, disabled, readonly). |
CTkEntry Methods
Besides the arguments, CTkEntry also has useful methods for handling user input:
.get()
: Retrieves the text entered in the Entry.
text_entered = self.entry.get()
print(text_entered)
.set(value)
: Sets a new value in the Entry.
self.entry.set("New Text")
.delete()
: Deletes the text inside the Entry.
self.entry.delete(0, "end") # Deletes all text
.bind()
: Binds events such as key presses to the Entry.
self.entry.bind("<Return>", lambda e: print("Enter pressed"))
Example: Creating an Input Form with a Button
In this example, the user enters their name in an Entry, and a button displays the name on the screen when clicked.
import customtkinter as ctk
# Main class
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.geometry("400x200")
self.title("Input Form")
# Function to display the entered name
def show_name():
name = self.entry.get() # Get text from Entry
self.label.configure(text=f"Hello, {name}!") # Update label text
# Creating the Entry
self.entry = ctk.CTkEntry(self, placeholder_text="Enter your name", width=250)
self.entry.place(relx=0.5, rely=0.4, anchor="center")
# Creating the button to display the name
self.button = ctk.CTkButton(self, text="Show Name", command=show_name)
self.button.place(relx=0.5, rely=0.6, anchor="center")
# Label to display the greeting
self.label = ctk.CTkLabel(self, text="")
self.label.place(relx=0.5, rely=0.8, anchor="center")
# Initializing the App
app = App()
app.mainloop()
Example Description:
-
The Entry field receives the user's name.
-
The Button calls the
show_name
function, which retrieves the Entry text using.get()
and displays it in a Label.
Now you have both basic and advanced knowledge to create Entries in CustomTkinter, enabling you to build interactive and modern interfaces for your applications!
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/customtkinter/how-to-create-an-entry-in-customtkinter/
License Agreement:Attribution-NonCommercial 4.0 International License