2021-03-04 20:27:46 +03:00
|
|
|
import tkinter
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
from .customtkinter_tk import CTk
|
2021-03-04 20:27:46 +03:00
|
|
|
from .customtkinter_frame import CTkFrame
|
|
|
|
from .appearance_mode_tracker import AppearanceModeTracker
|
2022-02-23 00:38:40 +03:00
|
|
|
from .customtkinter_theme_manager import CTkThemeManager
|
2022-02-24 02:19:40 +03:00
|
|
|
from .customtkinter_canvas import CTkCanvas
|
|
|
|
from .customtkinter_settings import CTkSettings
|
2022-03-01 18:12:55 +03:00
|
|
|
from .customtkinter_draw_engine import CTkDrawEngine
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CTkEntry(tkinter.Frame):
|
2022-01-16 18:54:21 +03:00
|
|
|
def __init__(self, *args,
|
2021-03-04 20:27:46 +03:00
|
|
|
master=None,
|
|
|
|
bg_color=None,
|
2022-02-23 00:38:40 +03:00
|
|
|
fg_color="default_theme",
|
|
|
|
text_color="default_theme",
|
|
|
|
placeholder_text_color="default_theme",
|
2022-02-24 02:19:40 +03:00
|
|
|
text_font="default_theme",
|
2022-02-04 23:28:37 +03:00
|
|
|
placeholder_text=None,
|
2022-03-01 21:10:18 +03:00
|
|
|
corner_radius="default_theme",
|
|
|
|
border_width="default_theme",
|
2022-02-28 02:35:52 +03:00
|
|
|
border_color="default_theme",
|
2021-03-04 20:27:46 +03:00
|
|
|
width=120,
|
2022-01-07 22:54:45 +03:00
|
|
|
height=30,
|
2021-03-04 20:27:46 +03:00
|
|
|
**kwargs):
|
2022-01-16 18:54:21 +03:00
|
|
|
if master is None:
|
|
|
|
super().__init__(*args)
|
|
|
|
else:
|
|
|
|
super().__init__(*args, master=master)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
# overwrite configure methods of master when master is tkinter widget, so that bg changes get applied on child CTk widget too
|
|
|
|
if isinstance(self.master, (tkinter.Tk, tkinter.Frame)) and not isinstance(self.master, (CTk, CTkFrame)):
|
|
|
|
master_old_configure = self.master.config
|
|
|
|
|
|
|
|
def new_configure(*args, **kwargs):
|
|
|
|
if "bg" in kwargs:
|
|
|
|
self.configure(bg_color=kwargs["bg"])
|
|
|
|
elif "background" in kwargs:
|
|
|
|
self.configure(bg_color=kwargs["background"])
|
|
|
|
|
|
|
|
# args[0] is dict when attribute gets changed by widget[<attribut>] syntax
|
|
|
|
elif len(args) > 0 and type(args[0]) == dict:
|
|
|
|
if "bg" in args[0]:
|
|
|
|
self.configure(bg_color=args[0]["bg"])
|
|
|
|
elif "background" in args[0]:
|
|
|
|
self.configure(bg_color=args[0]["background"])
|
|
|
|
master_old_configure(*args, **kwargs)
|
|
|
|
|
|
|
|
self.master.config = new_configure
|
|
|
|
self.master.configure = new_configure
|
|
|
|
|
|
|
|
AppearanceModeTracker.add(self.change_appearance_mode, self)
|
2021-11-07 00:06:27 +03:00
|
|
|
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-18 01:23:47 +03:00
|
|
|
self.configure_basic_grid()
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
self.bg_color = self.detect_color_of_master() if bg_color is None else bg_color
|
2022-02-28 02:35:52 +03:00
|
|
|
self.fg_color = CTkThemeManager.theme["color"]["entry"] if fg_color == "default_theme" else fg_color
|
|
|
|
self.text_color = CTkThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
|
|
|
self.placeholder_text_color = CTkThemeManager.theme["color"]["entry_placeholder_text"] if placeholder_text_color == "default_theme" else placeholder_text_color
|
|
|
|
self.text_font = (CTkThemeManager.theme["text"]["font"], CTkThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
|
|
|
self.border_color = CTkThemeManager.theme["color"]["entry_border"] if border_color == "default_theme" else border_color
|
2022-01-30 01:55:48 +03:00
|
|
|
|
2022-02-04 23:28:37 +03:00
|
|
|
self.placeholder_text = placeholder_text
|
|
|
|
self.placeholder_text_active = False
|
|
|
|
self.pre_placeholder_arguments = {} # some set arguments of the entry will be changed for placeholder and then set back
|
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2022-02-28 02:35:52 +03:00
|
|
|
self.corner_radius = CTkThemeManager.theme["shape"]["button_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
|
|
|
self.border_width = CTkThemeManager.theme["shape"]["entry_border_width"] if border_width == "default_theme" else border_width
|
2021-11-10 02:03:08 +03:00
|
|
|
|
|
|
|
if self.corner_radius*2 > self.height:
|
|
|
|
self.corner_radius = self.height/2
|
|
|
|
elif self.corner_radius*2 > self.width:
|
|
|
|
self.corner_radius = self.width/2
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-16 18:54:21 +03:00
|
|
|
super().configure(width=self.width, height=self.height)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-02-24 02:19:40 +03:00
|
|
|
self.canvas = CTkCanvas(master=self,
|
|
|
|
highlightthickness=0,
|
|
|
|
width=self.width,
|
|
|
|
height=self.height)
|
2022-02-08 22:59:50 +03:00
|
|
|
self.canvas.grid(column=0, row=0, sticky="we")
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
self.entry = tkinter.Entry(master=self,
|
|
|
|
bd=0,
|
2022-01-18 01:23:47 +03:00
|
|
|
width=1,
|
2022-02-23 03:47:34 +03:00
|
|
|
highlightthickness=0,
|
2022-01-30 01:55:48 +03:00
|
|
|
font=self.text_font,
|
2022-01-16 18:54:21 +03:00
|
|
|
**kwargs)
|
2022-01-22 14:00:29 +03:00
|
|
|
self.entry.grid(column=0, row=0, sticky="we", padx=self.corner_radius if self.corner_radius >= 6 else 6)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-03-01 18:12:55 +03:00
|
|
|
self.draw_engine = CTkDrawEngine(self.canvas, CTkSettings.preferred_drawing_method)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-02-04 23:28:37 +03:00
|
|
|
super().bind('<Configure>', self.update_dimensions)
|
|
|
|
self.entry.bind('<FocusOut>', self.set_placeholder)
|
|
|
|
self.entry.bind('<FocusIn>', self.clear_placeholder)
|
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
self.draw()
|
2022-02-04 23:28:37 +03:00
|
|
|
self.set_placeholder()
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-02 00:16:06 +03:00
|
|
|
def destroy(self):
|
|
|
|
AppearanceModeTracker.remove(self.change_appearance_mode)
|
|
|
|
super().destroy()
|
|
|
|
|
2022-01-18 01:23:47 +03:00
|
|
|
def configure_basic_grid(self):
|
|
|
|
self.grid_rowconfigure(0, weight=1)
|
|
|
|
self.grid_columnconfigure(0, weight=1)
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
def detect_color_of_master(self):
|
|
|
|
if isinstance(self.master, CTkFrame):
|
|
|
|
return self.master.fg_color
|
|
|
|
else:
|
2022-02-17 18:39:39 +03:00
|
|
|
try:
|
|
|
|
return self.master.cget("bg")
|
|
|
|
except:
|
|
|
|
pass
|
2021-11-07 00:06:27 +03:00
|
|
|
|
2022-01-18 01:23:47 +03:00
|
|
|
def update_dimensions(self, event):
|
|
|
|
# only redraw if dimensions changed (for performance)
|
|
|
|
if self.width != event.width or self.height != event.height:
|
2022-01-23 19:06:34 +03:00
|
|
|
# print(event.x, event.width, self.width)
|
2022-01-18 01:23:47 +03:00
|
|
|
self.width = event.width
|
|
|
|
self.height = event.height
|
|
|
|
|
|
|
|
self.draw()
|
|
|
|
|
2022-02-04 23:28:37 +03:00
|
|
|
def set_placeholder(self, event=None):
|
|
|
|
if self.placeholder_text is not None:
|
|
|
|
if not self.placeholder_text_active and self.entry.get() == "":
|
|
|
|
self.placeholder_text_active = True
|
|
|
|
self.pre_placeholder_arguments = {"show": self.entry.cget("show")}
|
2022-02-23 00:38:40 +03:00
|
|
|
self.entry.config(fg=CTkThemeManager.single_color(self.placeholder_text_color, self.appearance_mode), show="")
|
2022-02-04 23:28:37 +03:00
|
|
|
self.entry.delete(0, tkinter.END)
|
|
|
|
self.entry.insert(0, self.placeholder_text)
|
|
|
|
|
|
|
|
def clear_placeholder(self, event=None):
|
|
|
|
if self.placeholder_text_active:
|
|
|
|
self.placeholder_text_active = False
|
2022-02-23 00:38:40 +03:00
|
|
|
self.entry.config(fg=CTkThemeManager.single_color(self.text_color, self.appearance_mode))
|
2022-02-04 23:28:37 +03:00
|
|
|
self.entry.delete(0, tkinter.END)
|
|
|
|
for argument, value in self.pre_placeholder_arguments.items():
|
|
|
|
self.entry[argument] = value
|
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
def draw(self):
|
2022-02-24 02:19:40 +03:00
|
|
|
self.canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-02-28 02:35:52 +03:00
|
|
|
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.width, self.height, self.corner_radius, self.border_width)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-03-01 18:12:55 +03:00
|
|
|
if CTkThemeManager.single_color(self.fg_color, self.appearance_mode) is not None:
|
|
|
|
self.canvas.itemconfig("inner_parts",
|
|
|
|
fill=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
|
|
|
|
outline=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
|
|
|
|
self.entry.configure(bg=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
|
|
|
|
highlightcolor=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
|
|
|
|
fg=CTkThemeManager.single_color(self.text_color, self.appearance_mode),
|
|
|
|
insertbackground=CTkThemeManager.single_color(self.text_color, self.appearance_mode))
|
|
|
|
else:
|
|
|
|
self.canvas.itemconfig("inner_parts",
|
|
|
|
fill=CTkThemeManager.single_color(self.bg_color, self.appearance_mode),
|
|
|
|
outline=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
|
|
|
|
self.entry.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode),
|
|
|
|
highlightcolor=CTkThemeManager.single_color(self.bg_color, self.appearance_mode),
|
|
|
|
fg=CTkThemeManager.single_color(self.text_color, self.appearance_mode),
|
|
|
|
insertbackground=CTkThemeManager.single_color(self.text_color, self.appearance_mode))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-02-28 02:35:52 +03:00
|
|
|
self.canvas.itemconfig("border_parts",
|
|
|
|
fill=CTkThemeManager.single_color(self.border_color, self.appearance_mode),
|
|
|
|
outline=CTkThemeManager.single_color(self.border_color, self.appearance_mode))
|
|
|
|
|
2022-03-02 02:53:58 +03:00
|
|
|
if self.placeholder_text_active:
|
|
|
|
self.entry.config(fg=CTkThemeManager.single_color(self.placeholder_text_color, self.appearance_mode))
|
|
|
|
|
2022-02-04 23:28:37 +03:00
|
|
|
def bind(self, *args, **kwargs):
|
|
|
|
self.entry.bind(*args, **kwargs)
|
|
|
|
|
2021-11-10 22:54:53 +03:00
|
|
|
def config(self, *args, **kwargs):
|
|
|
|
self.configure(*args, **kwargs)
|
|
|
|
|
|
|
|
def configure(self, *args, **kwargs):
|
2022-01-07 22:54:45 +03:00
|
|
|
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
|
|
|
|
|
|
|
if "bg_color" in kwargs:
|
|
|
|
self.bg_color = kwargs["bg_color"]
|
|
|
|
del kwargs["bg_color"]
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "fg_color" in kwargs:
|
|
|
|
self.fg_color = kwargs["fg_color"]
|
|
|
|
del kwargs["fg_color"]
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "text_color" in kwargs:
|
|
|
|
self.text_color = kwargs["text_color"]
|
|
|
|
del kwargs["text_color"]
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "corner_radius" in kwargs:
|
2022-02-24 02:19:40 +03:00
|
|
|
self.corner_radius = kwargs["corner_radius"]
|
2022-01-18 01:23:47 +03:00
|
|
|
|
|
|
|
if self.corner_radius * 2 > self.height:
|
|
|
|
self.corner_radius = self.height / 2
|
|
|
|
elif self.corner_radius * 2 > self.width:
|
|
|
|
self.corner_radius = self.width / 2
|
|
|
|
|
2022-01-22 14:00:29 +03:00
|
|
|
self.entry.grid(column=0, row=0, sticky="we", padx=self.corner_radius if self.corner_radius >= 6 else 6)
|
2022-01-07 22:54:45 +03:00
|
|
|
del kwargs["corner_radius"]
|
|
|
|
require_redraw = True
|
|
|
|
|
2022-03-08 17:03:47 +03:00
|
|
|
if "placeholder_text" in kwargs:
|
|
|
|
pass
|
|
|
|
|
2022-01-16 18:54:21 +03:00
|
|
|
self.entry.configure(*args, **kwargs)
|
2021-11-10 22:54:53 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if require_redraw is True:
|
|
|
|
self.draw()
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
2022-02-04 23:28:37 +03:00
|
|
|
self.entry.delete(*args, **kwargs)
|
|
|
|
self.set_placeholder()
|
|
|
|
return
|
2022-01-07 22:54:45 +03:00
|
|
|
|
|
|
|
def insert(self, *args, **kwargs):
|
2022-02-04 23:28:37 +03:00
|
|
|
self.clear_placeholder()
|
2022-01-07 22:54:45 +03:00
|
|
|
return self.entry.insert(*args, **kwargs)
|
|
|
|
|
|
|
|
def get(self):
|
2022-02-04 23:28:37 +03:00
|
|
|
if self.placeholder_text_active:
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
return self.entry.get()
|
2022-01-07 22:54:45 +03:00
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
def change_appearance_mode(self, mode_string):
|
|
|
|
if mode_string.lower() == "dark":
|
|
|
|
self.appearance_mode = 1
|
|
|
|
elif mode_string.lower() == "light":
|
|
|
|
self.appearance_mode = 0
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if isinstance(self.master, (CTkFrame, CTk)):
|
2021-03-04 20:27:46 +03:00
|
|
|
self.bg_color = self.master.fg_color
|
|
|
|
else:
|
|
|
|
self.bg_color = self.master.cget("bg")
|
|
|
|
|
|
|
|
self.draw()
|