Icons to add in Entry #2400
-
How can i add an icon, like I am creating a login page and I want to add icons. |
Beta Was this translation helpful? Give feedback.
Answered by
rigvedmaanas
May 8, 2024
Replies: 1 comment
-
Hi @ubaid2751, There is no inbuilt option to add icon in entry. But you can create a custom component with label (for icon) and entry. Example Code from customtkinter import *
from PIL import Image
class IconEntry(CTkFrame):
def __init__(self, *args, icon, placeholder_text="", **kwargs):
super().__init__(*args, **kwargs)
self.configure(cursor="xterm")
self.icon = CTkLabel(master=self, text="", image=icon, cursor="xterm")
self.icon.pack(pady=(5, 5), padx=10, side="left")
self.entry = CTkEntry(master=self, placeholder_text=placeholder_text, corner_radius=0, border_width=0, fg_color=super().cget("fg_color"))
self.entry.pack(expand=True, fill="x", padx=(0, 10), side="left")
self.icon.bind("<Button-1>", lambda e: self.entry.focus_set())
self.bind("<Button-1>", lambda e: self.entry.focus_set())
def get(self):
return self.entry.get()
def insert(self, index, string):
self.entry.insert(index, string)
def delete(self, first_index, last_index):
self.entry.delete(first_index, last_index)
def configure(self, require_redraw=False, **kwargs):
if "icon" in kwargs:
icon_image = kwargs.pop("icon")
self.icon.configure(image=icon_image)
if "placeholder_text" in kwargs:
self.entry.configure(placeholder_text=kwargs.pop("placeholder_text"))
if "fg_color" in kwargs:
self.entry.configure(fg_color=kwargs["fg_color"])
super().configure(**kwargs, require_redraw=require_redraw)
set_default_color_theme("blue")
root = CTk()
root.geometry("500x500")
icon_entry = IconEntry(root, icon=CTkImage(Image.open("Path/to/your/icon.png"), size=(18, 18)))
icon_entry.pack(expand=True, fill="x", padx=50)
root.mainloop() |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ubaid2751
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @ubaid2751,
There is no inbuilt option to add icon in entry. But you can create a custom component with label (for icon) and entry.
Example Code