2022-05-31 23:32:21 +03:00
|
|
|
import tkinter
|
|
|
|
import sys
|
2022-12-06 20:47:39 +03:00
|
|
|
import copy
|
2022-11-11 01:27:55 +03:00
|
|
|
from typing import Union, Tuple, Callable, List, Optional
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-11-29 21:06:33 +03:00
|
|
|
from .core_widget_classes import DropdownMenu
|
|
|
|
from .core_rendering import CTkCanvas
|
|
|
|
from .theme import ThemeManager
|
|
|
|
from .core_rendering import DrawEngine
|
|
|
|
from .core_widget_classes import CTkBaseClass
|
|
|
|
from .font import CTkFont
|
2022-05-31 23:32:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CTkComboBox(CTkBaseClass):
|
2022-10-02 04:23:10 +03:00
|
|
|
"""
|
|
|
|
Combobox with dropdown menu, rounded corners, border, variable support.
|
|
|
|
For detailed information check out the documentation.
|
|
|
|
"""
|
|
|
|
|
2022-10-06 16:53:52 +03:00
|
|
|
def __init__(self,
|
2022-11-11 01:27:55 +03:00
|
|
|
master: any,
|
2022-10-03 01:33:06 +03:00
|
|
|
width: int = 140,
|
|
|
|
height: int = 28,
|
2022-11-11 01:27:55 +03:00
|
|
|
corner_radius: Optional[int] = None,
|
|
|
|
border_width: Optional[int] = None,
|
|
|
|
|
|
|
|
bg_color: Union[str, Tuple[str, str]] = "transparent",
|
|
|
|
fg_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
border_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
button_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
button_hover_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
dropdown_fg_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
dropdown_hover_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
dropdown_text_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
text_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
text_color_disabled: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
|
|
|
|
font: Optional[Union[tuple, CTkFont]] = None,
|
|
|
|
dropdown_font: Optional[Union[tuple, CTkFont]] = None,
|
|
|
|
values: Optional[List[str]] = None,
|
2022-10-02 04:23:10 +03:00
|
|
|
state: str = tkinter.NORMAL,
|
2022-10-03 01:33:06 +03:00
|
|
|
hover: bool = True,
|
2022-11-11 01:27:55 +03:00
|
|
|
variable: Union[tkinter.Variable, None] = None,
|
|
|
|
command: Union[Callable[[str], None], None] = None,
|
2022-10-22 15:24:04 +03:00
|
|
|
justify: str = "left",
|
2022-05-31 23:32:21 +03:00
|
|
|
**kwargs):
|
|
|
|
|
2022-11-01 02:37:30 +03:00
|
|
|
# transfer basic functionality (_bg_color, size, __appearance_mode, scaling) to CTkBaseClass
|
2022-10-06 16:53:52 +03:00
|
|
|
super().__init__(master=master, bg_color=bg_color, width=width, height=height, **kwargs)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
|
|
|
# shape
|
2022-11-27 04:48:09 +03:00
|
|
|
self._corner_radius = ThemeManager.theme["CTkComboBox"]["corner_radius"] if corner_radius is None else corner_radius
|
|
|
|
self._border_width = ThemeManager.theme["CTkComboBox"]["border_width"] if border_width is None else border_width
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
# color
|
2022-11-27 04:48:09 +03:00
|
|
|
self._fg_color = ThemeManager.theme["CTkComboBox"]["fg_color"] if fg_color is None else self._check_color_type(fg_color)
|
|
|
|
self._border_color = ThemeManager.theme["CTkComboBox"]["border_color"] if border_color is None else self._check_color_type(border_color)
|
|
|
|
self._button_color = ThemeManager.theme["CTkComboBox"]["button_color"] if button_color is None else self._check_color_type(button_color)
|
|
|
|
self._button_hover_color = ThemeManager.theme["CTkComboBox"]["button_hover_color"] if button_hover_color is None else self._check_color_type(button_hover_color)
|
|
|
|
self._text_color = ThemeManager.theme["CTkComboBox"]["text_color"] if text_color is None else self._check_color_type(text_color)
|
|
|
|
self._text_color_disabled = ThemeManager.theme["CTkComboBox"]["text_color_disabled"] if text_color_disabled is None else self._check_color_type(text_color_disabled)
|
2022-10-21 22:54:42 +03:00
|
|
|
|
|
|
|
# font
|
2022-11-11 01:27:55 +03:00
|
|
|
self._font = CTkFont() if font is None else self._check_font_type(font)
|
2022-10-21 22:54:42 +03:00
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.add_size_configure_callback(self._update_font)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
|
|
|
# callback and hover functionality
|
2022-10-02 04:23:10 +03:00
|
|
|
self._command = command
|
2022-10-03 01:33:06 +03:00
|
|
|
self._variable = variable
|
2022-10-02 04:23:10 +03:00
|
|
|
self._state = state
|
|
|
|
self._hover = hover
|
2022-05-31 23:32:21 +03:00
|
|
|
|
|
|
|
if values is None:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._values = ["CTkComboBox"]
|
2022-05-31 23:32:21 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._values = values
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._dropdown_menu = DropdownMenu(master=self,
|
|
|
|
values=self._values,
|
|
|
|
command=self._dropdown_callback,
|
2022-10-03 01:33:06 +03:00
|
|
|
fg_color=dropdown_fg_color,
|
2022-10-02 04:23:10 +03:00
|
|
|
hover_color=dropdown_hover_color,
|
|
|
|
text_color=dropdown_text_color,
|
2022-10-22 15:24:04 +03:00
|
|
|
font=dropdown_font)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
|
|
|
# configure grid system (1x1)
|
|
|
|
self.grid_rowconfigure(0, weight=1)
|
|
|
|
self.grid_columnconfigure(0, weight=1)
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas = CTkCanvas(master=self,
|
|
|
|
highlightthickness=0,
|
|
|
|
width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
|
|
|
self.draw_engine = DrawEngine(self._canvas)
|
|
|
|
|
|
|
|
self._entry = tkinter.Entry(master=self,
|
|
|
|
state=self._state,
|
|
|
|
width=1,
|
|
|
|
bd=0,
|
2022-10-22 15:24:04 +03:00
|
|
|
justify=justify,
|
2022-10-02 04:23:10 +03:00
|
|
|
highlightthickness=0,
|
2022-10-03 01:33:06 +03:00
|
|
|
font=self._apply_font_scaling(self._font))
|
2022-10-21 22:54:42 +03:00
|
|
|
|
2022-10-22 02:20:08 +03:00
|
|
|
self._create_grid()
|
2022-12-06 20:09:20 +03:00
|
|
|
self._create_bindings()
|
|
|
|
self._draw() # initial draw
|
|
|
|
|
|
|
|
if self._variable is not None:
|
|
|
|
self._entry.configure(textvariable=self._variable)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-09-16 00:55:55 +03:00
|
|
|
# insert default value
|
2022-12-08 00:10:35 +03:00
|
|
|
if self._variable is None:
|
|
|
|
if len(self._values) > 0:
|
|
|
|
self._entry.insert(0, self._values[0])
|
|
|
|
else:
|
|
|
|
self._entry.insert(0, "CTkComboBox")
|
2022-06-23 22:05:44 +03:00
|
|
|
|
2022-12-06 20:09:20 +03:00
|
|
|
def _create_bindings(self, sequence: Optional[str] = None):
|
|
|
|
""" set necessary bindings for functionality of widget, will overwrite other bindings """
|
|
|
|
if sequence is None:
|
|
|
|
self._canvas.tag_bind("right_parts", "<Enter>", self._on_enter)
|
|
|
|
self._canvas.tag_bind("dropdown_arrow", "<Enter>", self._on_enter)
|
|
|
|
self._canvas.tag_bind("right_parts", "<Leave>", self._on_leave)
|
|
|
|
self._canvas.tag_bind("dropdown_arrow", "<Leave>", self._on_leave)
|
|
|
|
self._canvas.tag_bind("right_parts", "<Button-1>", self._clicked)
|
|
|
|
self._canvas.tag_bind("dropdown_arrow", "<Button-1>", self._clicked)
|
2022-06-02 00:50:50 +03:00
|
|
|
|
2022-10-22 02:20:08 +03:00
|
|
|
def _create_grid(self):
|
2022-10-21 22:54:42 +03:00
|
|
|
self._canvas.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="nsew")
|
|
|
|
|
|
|
|
left_section_width = self._current_width - self._current_height
|
|
|
|
self._entry.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="ew",
|
|
|
|
padx=(max(self._apply_widget_scaling(self._corner_radius), self._apply_widget_scaling(3)),
|
|
|
|
max(self._apply_widget_scaling(self._current_width - left_section_width + 3), self._apply_widget_scaling(3))),
|
2022-10-22 02:20:08 +03:00
|
|
|
pady=self._apply_widget_scaling(self._border_width))
|
2022-10-21 22:54:42 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _set_scaling(self, *args, **kwargs):
|
|
|
|
super()._set_scaling(*args, **kwargs)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-06-28 12:16:28 +03:00
|
|
|
# change entry font size and grid padding
|
2022-10-03 01:33:06 +03:00
|
|
|
self._entry.configure(font=self._apply_font_scaling(self._font))
|
2022-10-22 02:20:08 +03:00
|
|
|
self._create_grid()
|
2022-06-28 12:16:28 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
2022-10-15 02:02:54 +03:00
|
|
|
self._draw(no_color_updates=True)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _set_dimensions(self, width: int = None, height: int = None):
|
|
|
|
super()._set_dimensions(width, height)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
|
|
|
self._draw()
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-10-21 22:54:42 +03:00
|
|
|
def _update_font(self):
|
|
|
|
""" pass font to tkinter widgets with applied font scaling and update grid with workaround """
|
|
|
|
self._entry.configure(font=self._apply_font_scaling(self._font))
|
|
|
|
|
|
|
|
# Workaround to force grid to be resized when text changes size.
|
|
|
|
# Otherwise grid will lag and only resizes if other mouse action occurs.
|
|
|
|
self._canvas.grid_forget()
|
2022-10-22 02:20:08 +03:00
|
|
|
self._canvas.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="nsew")
|
2022-10-21 22:54:42 +03:00
|
|
|
|
|
|
|
def destroy(self):
|
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.remove_size_configure_callback(self._update_font)
|
|
|
|
|
|
|
|
super().destroy()
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _draw(self, no_color_updates=False):
|
2022-11-01 02:37:30 +03:00
|
|
|
super()._draw(no_color_updates)
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
left_section_width = self._current_width - self._current_height
|
2022-10-02 04:23:10 +03:00
|
|
|
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border_vertical_split(self._apply_widget_scaling(self._current_width),
|
|
|
|
self._apply_widget_scaling(self._current_height),
|
|
|
|
self._apply_widget_scaling(self._corner_radius),
|
|
|
|
self._apply_widget_scaling(self._border_width),
|
|
|
|
self._apply_widget_scaling(left_section_width))
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
requires_recoloring_2 = self.draw_engine.draw_dropdown_arrow(self._apply_widget_scaling(self._current_width - (self._current_height / 2)),
|
|
|
|
self._apply_widget_scaling(self._current_height / 2),
|
|
|
|
self._apply_widget_scaling(self._current_height / 3))
|
2022-05-31 23:32:21 +03:00
|
|
|
|
|
|
|
if no_color_updates is False or requires_recoloring or requires_recoloring_2:
|
|
|
|
|
2022-10-29 02:20:32 +03:00
|
|
|
self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
|
|
|
|
self._canvas.itemconfig("inner_parts_left",
|
2022-10-29 02:20:32 +03:00
|
|
|
outline=self._apply_appearance_mode(self._fg_color),
|
|
|
|
fill=self._apply_appearance_mode(self._fg_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("border_parts_left",
|
2022-10-29 02:20:32 +03:00
|
|
|
outline=self._apply_appearance_mode(self._border_color),
|
|
|
|
fill=self._apply_appearance_mode(self._border_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("inner_parts_right",
|
2022-11-27 04:48:09 +03:00
|
|
|
outline=self._apply_appearance_mode(self._button_color),
|
|
|
|
fill=self._apply_appearance_mode(self._button_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("border_parts_right",
|
2022-11-27 04:48:09 +03:00
|
|
|
outline=self._apply_appearance_mode(self._button_color),
|
|
|
|
fill=self._apply_appearance_mode(self._button_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
|
2022-10-29 02:20:32 +03:00
|
|
|
self._entry.configure(bg=self._apply_appearance_mode(self._fg_color),
|
|
|
|
fg=self._apply_appearance_mode(self._text_color),
|
2023-01-10 17:03:45 +03:00
|
|
|
readonlybackground=self._apply_appearance_mode(self._fg_color),
|
2022-10-29 02:20:32 +03:00
|
|
|
disabledbackground=self._apply_appearance_mode(self._fg_color),
|
|
|
|
disabledforeground=self._apply_appearance_mode(self._text_color_disabled),
|
|
|
|
highlightcolor=self._apply_appearance_mode(self._fg_color),
|
|
|
|
insertbackground=self._apply_appearance_mode(self._text_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
|
|
|
|
if self._state == tkinter.DISABLED:
|
|
|
|
self._canvas.itemconfig("dropdown_arrow",
|
2022-10-29 02:20:32 +03:00
|
|
|
fill=self._apply_appearance_mode(self._text_color_disabled))
|
2022-05-31 23:32:21 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("dropdown_arrow",
|
2022-10-29 02:20:32 +03:00
|
|
|
fill=self._apply_appearance_mode(self._text_color))
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _open_dropdown_menu(self):
|
|
|
|
self._dropdown_menu.open(self.winfo_rootx(),
|
|
|
|
self.winfo_rooty() + self._apply_widget_scaling(self._current_height + 0))
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
def configure(self, require_redraw=False, **kwargs):
|
2022-10-21 22:54:42 +03:00
|
|
|
if "corner_radius" in kwargs:
|
|
|
|
self._corner_radius = kwargs.pop("corner_radius")
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "border_width" in kwargs:
|
|
|
|
self._border_width = kwargs.pop("border_width")
|
2022-10-22 02:20:08 +03:00
|
|
|
self._create_grid()
|
2022-10-21 22:54:42 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
if "fg_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._fg_color = self._check_color_type(kwargs.pop("fg_color"))
|
2022-05-31 23:32:21 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-23 14:18:48 +03:00
|
|
|
if "border_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._border_color = self._check_color_type(kwargs.pop("border_color"))
|
2022-10-23 14:18:48 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
if "button_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._button_color = self._check_color_type(kwargs.pop("button_color"))
|
2022-05-31 23:32:21 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "button_hover_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._button_hover_color = self._check_color_type(kwargs.pop("button_hover_color"))
|
2022-05-31 23:32:21 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-23 14:18:48 +03:00
|
|
|
if "dropdown_fg_color" in kwargs:
|
|
|
|
self._dropdown_menu.configure(fg_color=kwargs.pop("dropdown_fg_color"))
|
|
|
|
|
|
|
|
if "dropdown_hover_color" in kwargs:
|
|
|
|
self._dropdown_menu.configure(hover_color=kwargs.pop("dropdown_hover_color"))
|
|
|
|
|
|
|
|
if "dropdown_text_color" in kwargs:
|
|
|
|
self._dropdown_menu.configure(text_color=kwargs.pop("dropdown_text_color"))
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
if "text_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._text_color = self._check_color_type(kwargs.pop("text_color"))
|
2022-05-31 23:32:21 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-23 14:18:48 +03:00
|
|
|
if "text_color_disabled" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._text_color_disabled = self._check_color_type(kwargs.pop("text_color_disabled"))
|
2022-10-23 14:18:48 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
if "font" in kwargs:
|
2022-10-21 22:54:42 +03:00
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.remove_size_configure_callback(self._update_font)
|
|
|
|
self._font = self._check_font_type(kwargs.pop("font"))
|
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.add_size_configure_callback(self._update_font)
|
|
|
|
|
|
|
|
self._update_font()
|
2022-08-16 15:05:15 +03:00
|
|
|
|
2022-10-23 14:18:48 +03:00
|
|
|
if "dropdown_font" in kwargs:
|
|
|
|
self._dropdown_menu.configure(font=kwargs.pop("dropdown_font"))
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-06-02 00:50:50 +03:00
|
|
|
if "values" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._values = kwargs.pop("values")
|
|
|
|
self._dropdown_menu.configure(values=self._values)
|
2022-06-15 02:24:02 +03:00
|
|
|
|
2022-10-23 14:18:48 +03:00
|
|
|
if "state" in kwargs:
|
|
|
|
self._state = kwargs.pop("state")
|
|
|
|
self._entry.configure(state=self._state)
|
|
|
|
require_redraw = True
|
2022-06-15 02:24:02 +03:00
|
|
|
|
2022-10-23 14:18:48 +03:00
|
|
|
if "hover" in kwargs:
|
|
|
|
self._hover = kwargs.pop("hover")
|
2022-06-15 02:24:02 +03:00
|
|
|
|
2022-10-23 14:18:48 +03:00
|
|
|
if "variable" in kwargs:
|
|
|
|
self._variable = kwargs.pop("variable")
|
|
|
|
self._entry.configure(textvariable=self._variable)
|
2022-06-15 02:24:02 +03:00
|
|
|
|
2022-10-23 14:18:48 +03:00
|
|
|
if "command" in kwargs:
|
|
|
|
self._command = kwargs.pop("command")
|
2022-10-22 15:24:04 +03:00
|
|
|
|
|
|
|
if "justify" in kwargs:
|
|
|
|
self._entry.configure(justify=kwargs.pop("justify"))
|
2022-06-02 00:50:50 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
super().configure(require_redraw=require_redraw, **kwargs)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
def cget(self, attribute_name: str) -> any:
|
|
|
|
if attribute_name == "corner_radius":
|
|
|
|
return self._corner_radius
|
|
|
|
elif attribute_name == "border_width":
|
|
|
|
return self._border_width
|
|
|
|
|
|
|
|
elif attribute_name == "fg_color":
|
|
|
|
return self._fg_color
|
|
|
|
elif attribute_name == "border_color":
|
|
|
|
return self._border_color
|
|
|
|
elif attribute_name == "button_color":
|
|
|
|
return self._button_color
|
|
|
|
elif attribute_name == "button_hover_color":
|
|
|
|
return self._button_hover_color
|
|
|
|
elif attribute_name == "dropdown_fg_color":
|
|
|
|
return self._dropdown_menu.cget("fg_color")
|
|
|
|
elif attribute_name == "dropdown_hover_color":
|
|
|
|
return self._dropdown_menu.cget("hover_color")
|
|
|
|
elif attribute_name == "dropdown_text_color":
|
|
|
|
return self._dropdown_menu.cget("text_color")
|
|
|
|
elif attribute_name == "text_color":
|
|
|
|
return self._text_color
|
|
|
|
elif attribute_name == "text_color_disabled":
|
|
|
|
return self._text_color_disabled
|
|
|
|
|
|
|
|
elif attribute_name == "font":
|
|
|
|
return self._font
|
2022-10-22 15:24:04 +03:00
|
|
|
elif attribute_name == "dropdown_font":
|
|
|
|
return self._dropdown_menu.cget("font")
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "values":
|
2022-12-06 20:47:39 +03:00
|
|
|
return copy.copy(self._values)
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "state":
|
|
|
|
return self._state
|
|
|
|
elif attribute_name == "hover":
|
|
|
|
return self._hover
|
|
|
|
elif attribute_name == "variable":
|
|
|
|
return self._variable
|
|
|
|
elif attribute_name == "command":
|
|
|
|
return self._command
|
2022-10-22 15:24:04 +03:00
|
|
|
elif attribute_name == "justify":
|
|
|
|
return self._entry.cget("justify")
|
2022-10-03 01:33:06 +03:00
|
|
|
else:
|
|
|
|
return super().cget(attribute_name)
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _on_enter(self, event=0):
|
|
|
|
if self._hover is True and self._state == tkinter.NORMAL and len(self._values) > 0:
|
2022-10-22 15:24:04 +03:00
|
|
|
if sys.platform == "darwin" and len(self._values) > 0 and self._cursor_manipulation_enabled:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(cursor="pointinghand")
|
2022-10-22 15:24:04 +03:00
|
|
|
elif sys.platform.startswith("win") and len(self._values) > 0 and self._cursor_manipulation_enabled:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(cursor="hand2")
|
2022-05-31 23:32:21 +03:00
|
|
|
|
|
|
|
# set color of inner button parts to hover color
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("inner_parts_right",
|
2022-10-29 02:20:32 +03:00
|
|
|
outline=self._apply_appearance_mode(self._button_hover_color),
|
|
|
|
fill=self._apply_appearance_mode(self._button_hover_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("border_parts_right",
|
2022-10-29 02:20:32 +03:00
|
|
|
outline=self._apply_appearance_mode(self._button_hover_color),
|
|
|
|
fill=self._apply_appearance_mode(self._button_hover_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
|
|
|
|
def _on_leave(self, event=0):
|
2022-10-22 15:24:04 +03:00
|
|
|
if sys.platform == "darwin" and len(self._values) > 0 and self._cursor_manipulation_enabled:
|
|
|
|
self._canvas.configure(cursor="arrow")
|
|
|
|
elif sys.platform.startswith("win") and len(self._values) > 0 and self._cursor_manipulation_enabled:
|
|
|
|
self._canvas.configure(cursor="arrow")
|
|
|
|
|
|
|
|
# set color of inner button parts
|
|
|
|
self._canvas.itemconfig("inner_parts_right",
|
2022-10-29 02:20:32 +03:00
|
|
|
outline=self._apply_appearance_mode(self._button_color),
|
|
|
|
fill=self._apply_appearance_mode(self._button_color))
|
2022-10-22 15:24:04 +03:00
|
|
|
self._canvas.itemconfig("border_parts_right",
|
2022-10-29 02:20:32 +03:00
|
|
|
outline=self._apply_appearance_mode(self._button_color),
|
|
|
|
fill=self._apply_appearance_mode(self._button_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
|
|
|
|
def _dropdown_callback(self, value: str):
|
|
|
|
if self._state == "readonly":
|
|
|
|
self._entry.configure(state="normal")
|
|
|
|
self._entry.delete(0, tkinter.END)
|
|
|
|
self._entry.insert(0, value)
|
|
|
|
self._entry.configure(state="readonly")
|
2022-07-07 19:19:23 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._entry.delete(0, tkinter.END)
|
|
|
|
self._entry.insert(0, value)
|
2022-09-16 00:55:55 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._command is not None:
|
|
|
|
self._command(value)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-09-16 00:55:55 +03:00
|
|
|
def set(self, value: str):
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._state == "readonly":
|
|
|
|
self._entry.configure(state="normal")
|
|
|
|
self._entry.delete(0, tkinter.END)
|
|
|
|
self._entry.insert(0, value)
|
|
|
|
self._entry.configure(state="readonly")
|
2022-09-16 00:55:55 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._entry.delete(0, tkinter.END)
|
|
|
|
self._entry.insert(0, value)
|
2022-05-31 23:32:21 +03:00
|
|
|
|
|
|
|
def get(self) -> str:
|
2022-10-02 04:23:10 +03:00
|
|
|
return self._entry.get()
|
2022-05-31 23:32:21 +03:00
|
|
|
|
2022-12-06 20:09:20 +03:00
|
|
|
def _clicked(self, event=None):
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._state is not tkinter.DISABLED and len(self._values) > 0:
|
|
|
|
self._open_dropdown_menu()
|
2022-10-04 03:26:29 +03:00
|
|
|
|
2022-12-06 20:09:20 +03:00
|
|
|
def bind(self, sequence=None, command=None, add=True):
|
2022-10-04 04:03:43 +03:00
|
|
|
""" called on the tkinter.Entry """
|
2022-12-06 21:25:00 +03:00
|
|
|
if not (add == "+" or add is True):
|
2022-12-06 20:09:20 +03:00
|
|
|
raise ValueError("'add' argument can only be '+' or True to preserve internal callbacks")
|
|
|
|
self._entry.bind(sequence, command, add=True)
|
2022-10-04 04:03:43 +03:00
|
|
|
|
2022-12-06 20:09:20 +03:00
|
|
|
def unbind(self, sequence=None, funcid=None):
|
2022-10-04 04:03:43 +03:00
|
|
|
""" called on the tkinter.Entry """
|
2022-12-06 20:09:20 +03:00
|
|
|
if funcid is not None:
|
|
|
|
raise ValueError("'funcid' argument can only be None, because there is a bug in" +
|
|
|
|
" tkinter and its not clear whether the internal callbacks will be unbinded or not")
|
|
|
|
self._entry.unbind(sequence, None) # unbind all callbacks for sequence
|
|
|
|
self._create_bindings(sequence=sequence) # restore internal callbacks for sequence
|
2022-10-05 22:11:46 +03:00
|
|
|
|
|
|
|
def focus(self):
|
|
|
|
return self._entry.focus()
|
|
|
|
|
|
|
|
def focus_set(self):
|
|
|
|
return self._entry.focus_set()
|
|
|
|
|
|
|
|
def focus_force(self):
|
|
|
|
return self._entry.focus_force()
|