2021-03-04 20:27:46 +03:00
|
|
|
import tkinter
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
from .ctk_canvas import CTkCanvas
|
2022-05-17 22:44:59 +03:00
|
|
|
from ..theme_manager import ThemeManager
|
|
|
|
from ..draw_engine import DrawEngine
|
2022-04-20 23:50:57 +03:00
|
|
|
from .widget_base_class import CTkBaseClass
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
class CTkEntry(CTkBaseClass):
|
2022-01-16 18:54:21 +03:00
|
|
|
def __init__(self, *args,
|
2021-03-04 20:27:46 +03:00
|
|
|
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",
|
2022-05-31 23:32:21 +03:00
|
|
|
width=140,
|
2022-05-25 23:14:38 +03:00
|
|
|
height=28,
|
2022-05-22 20:45:42 +03:00
|
|
|
state=tkinter.NORMAL,
|
2022-07-07 19:07:54 +03:00
|
|
|
textvariable: tkinter.Variable = None,
|
2021-03-04 20:27:46 +03:00
|
|
|
**kwargs):
|
2022-01-07 22:54:45 +03:00
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
2022-04-20 23:50:57 +03:00
|
|
|
if "master" in kwargs:
|
2022-07-07 17:02:51 +03:00
|
|
|
super().__init__(*args, bg_color=bg_color, width=width, height=height, master=kwargs.pop("master"))
|
2022-04-20 23:50:57 +03:00
|
|
|
else:
|
|
|
|
super().__init__(*args, bg_color=bg_color, width=width, height=height)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-07-07 19:07:54 +03:00
|
|
|
# configure grid system (1x1)
|
2022-05-12 23:55:28 +03:00
|
|
|
self.grid_rowconfigure(0, weight=1)
|
|
|
|
self.grid_columnconfigure(0, weight=1)
|
2022-01-18 01:23:47 +03:00
|
|
|
|
2022-07-07 19:07:54 +03:00
|
|
|
# color
|
2022-05-17 22:44:59 +03:00
|
|
|
self.fg_color = ThemeManager.theme["color"]["entry"] if fg_color == "default_theme" else fg_color
|
|
|
|
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
|
|
|
self.placeholder_text_color = ThemeManager.theme["color"]["entry_placeholder_text"] if placeholder_text_color == "default_theme" else placeholder_text_color
|
|
|
|
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
|
|
|
self.border_color = ThemeManager.theme["color"]["entry_border"] if border_color == "default_theme" else border_color
|
2022-01-30 01:55:48 +03:00
|
|
|
|
2022-07-07 19:07:54 +03:00
|
|
|
# shape
|
|
|
|
self.corner_radius = ThemeManager.theme["shape"]["button_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
|
|
|
self.border_width = ThemeManager.theme["shape"]["entry_border_width"] if border_width == "default_theme" else border_width
|
|
|
|
|
|
|
|
# placeholder text
|
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
|
|
|
|
|
2022-07-07 19:07:54 +03:00
|
|
|
# textvariable
|
|
|
|
self.textvariable = textvariable
|
2022-05-22 20:45:42 +03:00
|
|
|
|
2022-07-07 19:07:54 +03:00
|
|
|
self.state = state
|
2021-11-10 02:03:08 +03:00
|
|
|
|
2022-02-24 02:19:40 +03:00
|
|
|
self.canvas = CTkCanvas(master=self,
|
|
|
|
highlightthickness=0,
|
2022-05-31 23:32:21 +03:00
|
|
|
width=self.apply_widget_scaling(self._current_width),
|
|
|
|
height=self.apply_widget_scaling(self._current_height))
|
2022-06-13 16:08:13 +03:00
|
|
|
self.canvas.grid(column=0, row=0, sticky="nswe")
|
2022-05-22 01:02:45 +03:00
|
|
|
self.draw_engine = DrawEngine(self.canvas)
|
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-04-21 19:34:58 +03:00
|
|
|
font=self.apply_font_scaling(self.text_font),
|
2022-05-22 20:45:42 +03:00
|
|
|
state=self.state,
|
2022-07-07 19:07:54 +03:00
|
|
|
textvariable=self.textvariable,
|
2022-01-16 18:54:21 +03:00
|
|
|
**kwargs)
|
2022-06-13 16:08:13 +03:00
|
|
|
self.entry.grid(column=0, row=0, sticky="nswe",
|
|
|
|
padx=self.apply_widget_scaling(self.corner_radius) if self.corner_radius >= 6 else self.apply_widget_scaling(6),
|
|
|
|
pady=(self.apply_widget_scaling(self.border_width), self.apply_widget_scaling(self.border_width + 1)))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-04-21 19:34:58 +03:00
|
|
|
super().bind('<Configure>', self.update_dimensions_event)
|
2022-07-01 22:30:31 +03:00
|
|
|
self.entry.bind('<FocusOut>', self.entry_focus_out)
|
|
|
|
self.entry.bind('<FocusIn>', self.entry_focus_in)
|
2022-02-04 23:28:37 +03:00
|
|
|
|
2022-09-13 22:47:50 +03:00
|
|
|
self.activate_placeholder()
|
2021-03-04 20:27:46 +03:00
|
|
|
self.draw()
|
2022-07-01 22:30:31 +03:00
|
|
|
|
2022-05-02 00:29:14 +03:00
|
|
|
def set_scaling(self, *args, **kwargs):
|
|
|
|
super().set_scaling( *args, **kwargs)
|
|
|
|
|
|
|
|
self.entry.configure(font=self.apply_font_scaling(self.text_font))
|
|
|
|
self.entry.grid(column=0, row=0, sticky="we",
|
|
|
|
padx=self.apply_widget_scaling(self.corner_radius) if self.corner_radius >= 6 else self.apply_widget_scaling(6))
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height))
|
2022-05-02 00:29:14 +03:00
|
|
|
self.draw()
|
|
|
|
|
2022-05-24 02:00:58 +03:00
|
|
|
def set_dimensions(self, width=None, height=None):
|
|
|
|
super().set_dimensions(width, height)
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
|
|
|
height=self.apply_widget_scaling(self._desired_height))
|
2022-05-24 02:00:58 +03:00
|
|
|
self.draw()
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
def draw(self, no_color_updates=False):
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self._current_width),
|
|
|
|
self.apply_widget_scaling(self._current_height),
|
2022-05-02 00:29:14 +03:00
|
|
|
self.apply_widget_scaling(self.corner_radius),
|
|
|
|
self.apply_widget_scaling(self.border_width))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-05-17 19:59:33 +03:00
|
|
|
if requires_recoloring or no_color_updates is False:
|
2022-05-31 23:32:21 +03:00
|
|
|
if ThemeManager.single_color(self.fg_color, self._appearance_mode) is not None:
|
2022-05-17 19:59:33 +03:00
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-05-31 23:32:21 +03:00
|
|
|
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
|
|
|
self.entry.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
|
|
|
disabledbackground=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
|
|
|
highlightcolor=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
|
|
|
fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
|
|
|
disabledforeground=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
|
|
|
insertbackground=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
2022-05-17 19:59:33 +03:00
|
|
|
else:
|
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-05-31 23:32:21 +03:00
|
|
|
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
|
|
|
self.entry.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
|
|
|
disabledbackground=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
|
|
|
highlightcolor=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
|
|
|
fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
|
|
|
disabledforeground=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
|
|
|
insertbackground=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
2022-05-17 19:59:33 +03:00
|
|
|
|
|
|
|
self.canvas.itemconfig("border_parts",
|
2022-05-31 23:32:21 +03:00
|
|
|
fill=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
2022-05-17 19:59:33 +03:00
|
|
|
|
|
|
|
if self.placeholder_text_active:
|
2022-05-31 23:32:21 +03:00
|
|
|
self.entry.config(fg=ThemeManager.single_color(self.placeholder_text_color, self._appearance_mode))
|
2022-03-02 02:53:58 +03:00
|
|
|
|
2022-02-04 23:28:37 +03:00
|
|
|
def bind(self, *args, **kwargs):
|
|
|
|
self.entry.bind(*args, **kwargs)
|
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
def configure(self, require_redraw=False, **kwargs):
|
2022-05-22 20:45:42 +03:00
|
|
|
if "state" in kwargs:
|
2022-07-01 22:30:31 +03:00
|
|
|
self.state = kwargs.pop("state")
|
2022-05-22 20:45:42 +03:00
|
|
|
self.entry.configure(state=self.state)
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if "fg_color" in kwargs:
|
2022-07-01 22:30:31 +03:00
|
|
|
self.fg_color = kwargs.pop("fg_color")
|
2022-01-07 22:54:45 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "text_color" in kwargs:
|
2022-07-01 22:30:31 +03:00
|
|
|
self.text_color = kwargs.pop("text_color")
|
2022-01-07 22:54:45 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-05-12 23:55:28 +03:00
|
|
|
if "border_color" in kwargs:
|
2022-07-01 22:30:31 +03:00
|
|
|
self.border_color = kwargs.pop("border_color")
|
2022-05-12 23:55:28 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if "corner_radius" in kwargs:
|
2022-07-01 22:30:31 +03:00
|
|
|
self.corner_radius = kwargs.pop("corner_radius")
|
2022-01-18 01:23:47 +03:00
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
if self.corner_radius * 2 > self._current_height:
|
|
|
|
self.corner_radius = self._current_height / 2
|
|
|
|
elif self.corner_radius * 2 > self._current_width:
|
|
|
|
self.corner_radius = self._current_width / 2
|
2022-01-18 01:23:47 +03:00
|
|
|
|
2022-05-02 00:29:14 +03:00
|
|
|
self.entry.grid(column=0, row=0, sticky="we", padx=self.apply_widget_scaling(self.corner_radius) if self.corner_radius >= 6 else self.apply_widget_scaling(6))
|
2022-01-07 22:54:45 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-05-24 02:00:58 +03:00
|
|
|
if "width" in kwargs:
|
2022-07-01 22:30:31 +03:00
|
|
|
self.set_dimensions(width=kwargs.pop("width"))
|
2022-05-24 02:00:58 +03:00
|
|
|
|
|
|
|
if "height" in kwargs:
|
2022-07-01 22:30:31 +03:00
|
|
|
self.set_dimensions(height=kwargs.pop("height"))
|
2022-05-24 02:00:58 +03:00
|
|
|
|
2022-03-08 17:03:47 +03:00
|
|
|
if "placeholder_text" in kwargs:
|
2022-07-01 22:30:31 +03:00
|
|
|
self.placeholder_text = kwargs.pop("placeholder_text")
|
|
|
|
if self.placeholder_text_active:
|
|
|
|
self.entry.delete(0, tkinter.END)
|
|
|
|
self.entry.insert(0, self.placeholder_text)
|
2022-09-13 22:47:50 +03:00
|
|
|
else:
|
|
|
|
self.activate_placeholder()
|
2022-07-01 22:30:31 +03:00
|
|
|
|
|
|
|
if "placeholder_text_color" in kwargs:
|
|
|
|
self.placeholder_text_color = kwargs.pop("placeholder_text_color")
|
|
|
|
require_redraw = True
|
|
|
|
|
2022-07-07 19:07:54 +03:00
|
|
|
if "textvariable" in kwargs:
|
|
|
|
self.textvariable = kwargs.pop("textvariable")
|
|
|
|
self.entry.configure(textvariable=self.textvariable)
|
|
|
|
|
2022-08-16 15:05:15 +03:00
|
|
|
if "text_font" in kwargs:
|
|
|
|
self.text_font = kwargs.pop("text_font")
|
|
|
|
self.entry.configure(font=self.apply_font_scaling(self.text_font))
|
|
|
|
|
2022-07-01 22:30:31 +03:00
|
|
|
if "show" in kwargs:
|
|
|
|
if self.placeholder_text_active:
|
|
|
|
self.pre_placeholder_arguments["show"] = kwargs.pop("show")
|
|
|
|
else:
|
|
|
|
self.entry.configure(show=kwargs.pop("show"))
|
2022-03-08 17:03:47 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
if "bg_color" in kwargs:
|
|
|
|
super().configure(bg_color=kwargs.pop("bg_color"), require_redraw=require_redraw)
|
|
|
|
else:
|
|
|
|
super().configure(require_redraw=require_redraw)
|
2021-11-10 22:54:53 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
self.entry.configure(**kwargs) # pass remaining kwargs to entry
|
2022-01-07 22:54:45 +03:00
|
|
|
|
2022-09-13 22:47:50 +03:00
|
|
|
def activate_placeholder(self):
|
2022-07-07 19:07:54 +03:00
|
|
|
if self.entry.get() == "" and self.placeholder_text is not None and (self.textvariable is None or self.textvariable == ""):
|
|
|
|
self.placeholder_text_active = True
|
|
|
|
|
|
|
|
self.pre_placeholder_arguments = {"show": self.entry.cget("show")}
|
|
|
|
self.entry.config(fg=ThemeManager.single_color(self.placeholder_text_color, self._appearance_mode), show="")
|
|
|
|
self.entry.delete(0, tkinter.END)
|
|
|
|
self.entry.insert(0, self.placeholder_text)
|
2022-07-01 22:30:31 +03:00
|
|
|
|
2022-09-13 22:47:50 +03:00
|
|
|
def deactivate_placeholder(self):
|
2022-07-07 19:07:54 +03:00
|
|
|
if self.placeholder_text_active:
|
|
|
|
self.placeholder_text_active = False
|
|
|
|
|
|
|
|
self.entry.config(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
|
|
|
self.entry.delete(0, tkinter.END)
|
|
|
|
for argument, value in self.pre_placeholder_arguments.items():
|
|
|
|
self.entry[argument] = value
|
2022-07-01 22:30:31 +03:00
|
|
|
|
|
|
|
def entry_focus_out(self, event=None):
|
2022-09-13 22:47:50 +03:00
|
|
|
self.activate_placeholder()
|
2022-07-01 22:30:31 +03:00
|
|
|
|
|
|
|
def entry_focus_in(self, event=None):
|
2022-09-13 22:47:50 +03:00
|
|
|
self.deactivate_placeholder()
|
2022-07-01 22:30:31 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
def delete(self, *args, **kwargs):
|
2022-02-04 23:28:37 +03:00
|
|
|
self.entry.delete(*args, **kwargs)
|
2022-07-01 22:30:31 +03:00
|
|
|
|
|
|
|
if self.entry.get() == "":
|
2022-09-13 22:47:50 +03:00
|
|
|
self.activate_placeholder()
|
2022-01-07 22:54:45 +03:00
|
|
|
|
|
|
|
def insert(self, *args, **kwargs):
|
2022-09-13 22:47:50 +03:00
|
|
|
self.deactivate_placeholder()
|
2022-07-01 22:30:31 +03:00
|
|
|
|
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-06-28 12:26:43 +03:00
|
|
|
|
|
|
|
def focus(self):
|
|
|
|
self.entry.focus()
|
|
|
|
|
|
|
|
def focus_force(self):
|
|
|
|
self.entry.focus_force()
|