2022-03-05 04:01:07 +03:00
|
|
|
import tkinter
|
|
|
|
import sys
|
2022-11-11 01:27:55 +03:00
|
|
|
from typing import Union, Tuple, Callable, Optional
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-11-29 21:06:33 +03:00
|
|
|
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-03-05 04:01:07 +03:00
|
|
|
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
class CTkRadioButton(CTkBaseClass):
|
2022-10-02 04:23:10 +03:00
|
|
|
"""
|
|
|
|
Radiobutton with rounded corners, border, label, variable support, command.
|
|
|
|
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-14 19:51:44 +03:00
|
|
|
width: int = 100,
|
2022-10-03 01:33:06 +03:00
|
|
|
height: int = 22,
|
2022-10-14 19:51:44 +03:00
|
|
|
radiobutton_width: int = 22,
|
|
|
|
radiobutton_height: int = 22,
|
2022-11-11 01:27:55 +03:00
|
|
|
corner_radius: Optional[int] = None,
|
|
|
|
border_width_unchecked: Optional[int] = None,
|
|
|
|
border_width_checked: Optional[int] = None,
|
2022-10-03 01:33:06 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
bg_color: Union[str, Tuple[str, str]] = "transparent",
|
|
|
|
fg_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
hover_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
border_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,
|
2022-10-03 01:33:06 +03:00
|
|
|
|
|
|
|
text: str = "CTkRadioButton",
|
2022-11-11 01:27:55 +03:00
|
|
|
font: Optional[Union[tuple, CTkFont]] = None,
|
|
|
|
textvariable: Union[tkinter.Variable, None] = None,
|
|
|
|
variable: Union[tkinter.Variable, None] = None,
|
2022-10-03 01:33:06 +03:00
|
|
|
value: Union[int, str] = 0,
|
|
|
|
state: str = tkinter.NORMAL,
|
2022-10-02 04:23:10 +03:00
|
|
|
hover: bool = True,
|
2022-11-11 01:27:55 +03:00
|
|
|
command: Union[Callable, None] = None,
|
2022-03-05 04:01:07 +03:00
|
|
|
**kwargs):
|
2022-04-20 23:50:57 +03:00
|
|
|
|
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-04-20 23:50:57 +03:00
|
|
|
|
2022-10-14 19:51:44 +03:00
|
|
|
# dimensions
|
|
|
|
self._radiobutton_width = radiobutton_width
|
|
|
|
self._radiobutton_height = radiobutton_height
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# color
|
2022-11-27 04:48:09 +03:00
|
|
|
self._fg_color = ThemeManager.theme["CTkRadiobutton"]["fg_color"] if fg_color is None else self._check_color_type(fg_color)
|
|
|
|
self._hover_color = ThemeManager.theme["CTkRadiobutton"]["hover_color"] if hover_color is None else self._check_color_type(hover_color)
|
|
|
|
self._border_color = ThemeManager.theme["CTkRadiobutton"]["border_color"] if border_color is None else self._check_color_type(border_color)
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# shape
|
2022-11-27 04:48:09 +03:00
|
|
|
self._corner_radius = ThemeManager.theme["CTkRadiobutton"]["corner_radius"] if corner_radius is None else corner_radius
|
|
|
|
self._border_width_unchecked = ThemeManager.theme["CTkRadiobutton"]["border_width_unchecked"] if border_width_unchecked is None else border_width_unchecked
|
|
|
|
self._border_width_checked = ThemeManager.theme["CTkRadiobutton"]["border_width_checked"] if border_width_checked is None else border_width_checked
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# text
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text = text
|
|
|
|
self._text_label: Union[tkinter.Label, None] = None
|
2022-11-27 04:48:09 +03:00
|
|
|
self._text_color = ThemeManager.theme["CTkRadiobutton"]["text_color"] if text_color is None else self._check_color_type(text_color)
|
|
|
|
self._text_color_disabled = ThemeManager.theme["CTkRadiobutton"]["text_color_disabled"] if text_color_disabled is None else self._check_color_type(text_color_disabled)
|
2022-10-22 16:08:13 +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-22 16:08:13 +03:00
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.add_size_configure_callback(self._update_font)
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# callback and control variables
|
2022-10-02 04:23:10 +03:00
|
|
|
self._command = command
|
|
|
|
self._state = state
|
|
|
|
self._hover = hover
|
|
|
|
self._check_state: bool = False
|
|
|
|
self._value = value
|
|
|
|
self._variable: tkinter.Variable = variable
|
|
|
|
self._variable_callback_blocked: bool = False
|
|
|
|
self._textvariable = textvariable
|
|
|
|
self._variable_callback_name: Union[str, None] = None
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# configure grid system (3x1)
|
2022-03-05 15:05:39 +03:00
|
|
|
self.grid_columnconfigure(0, weight=0)
|
2022-10-02 04:23:10 +03:00
|
|
|
self.grid_columnconfigure(1, weight=0, minsize=self._apply_widget_scaling(6))
|
2022-03-05 15:05:39 +03:00
|
|
|
self.grid_columnconfigure(2, weight=1)
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._bg_canvas = CTkCanvas(master=self,
|
|
|
|
highlightthickness=0,
|
|
|
|
width=self._apply_widget_scaling(self._current_width),
|
|
|
|
height=self._apply_widget_scaling(self._current_height))
|
2022-10-14 19:51:44 +03:00
|
|
|
self._bg_canvas.grid(row=0, column=0, columnspan=3, sticky="nswe")
|
2022-10-02 04:23:10 +03:00
|
|
|
|
|
|
|
self._canvas = CTkCanvas(master=self,
|
|
|
|
highlightthickness=0,
|
2022-10-14 19:51:44 +03:00
|
|
|
width=self._apply_widget_scaling(self._radiobutton_width),
|
|
|
|
height=self._apply_widget_scaling(self._radiobutton_height))
|
|
|
|
self._canvas.grid(row=0, column=0)
|
2022-10-02 04:23:10 +03:00
|
|
|
self._draw_engine = DrawEngine(self._canvas)
|
|
|
|
|
|
|
|
self._text_label = tkinter.Label(master=self,
|
|
|
|
bd=0,
|
2022-10-14 19:51:44 +03:00
|
|
|
padx=0,
|
|
|
|
pady=0,
|
2022-10-02 04:23:10 +03:00
|
|
|
text=self._text,
|
|
|
|
justify=tkinter.LEFT,
|
2022-10-03 01:33:06 +03:00
|
|
|
font=self._apply_font_scaling(self._font),
|
2022-10-02 04:23:10 +03:00
|
|
|
textvariable=self._textvariable)
|
2022-10-14 19:51:44 +03:00
|
|
|
self._text_label.grid(row=0, column=2, sticky="w")
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text_label["anchor"] = "w"
|
|
|
|
|
|
|
|
if self._variable is not None:
|
|
|
|
self._variable_callback_name = self._variable.trace_add("write", self._variable_callback)
|
|
|
|
self._check_state = True if self._variable.get() == self._value else False
|
|
|
|
|
2022-12-06 20:09:20 +03:00
|
|
|
self._create_bindings()
|
2022-10-02 04:23:10 +03:00
|
|
|
self._set_cursor()
|
2022-12-06 20:09:20 +03:00
|
|
|
self._draw()
|
|
|
|
|
|
|
|
def _create_bindings(self, sequence: Optional[str] = None):
|
|
|
|
""" set necessary bindings for functionality of widget, will overwrite other bindings """
|
|
|
|
if sequence is None or sequence == "<Enter>":
|
|
|
|
self._canvas.bind("<Enter>", self._on_enter)
|
|
|
|
self._text_label.bind("<Enter>", self._on_enter)
|
|
|
|
if sequence is None or sequence == "<Leave>":
|
|
|
|
self._canvas.bind("<Leave>", self._on_leave)
|
|
|
|
self._text_label.bind("<Leave>", self._on_leave)
|
|
|
|
if sequence is None or sequence == "<Button-1>":
|
|
|
|
self._canvas.bind("<Button-1>", self.invoke)
|
|
|
|
self._text_label.bind("<Button-1>", self.invoke)
|
2022-10-02 04:23:10 +03:00
|
|
|
|
|
|
|
def _set_scaling(self, *args, **kwargs):
|
|
|
|
super()._set_scaling(*args, **kwargs)
|
|
|
|
|
|
|
|
self.grid_columnconfigure(1, weight=0, minsize=self._apply_widget_scaling(6))
|
2022-10-03 01:33:06 +03:00
|
|
|
self._text_label.configure(font=self._apply_font_scaling(self._font))
|
2022-10-02 04:23:10 +03:00
|
|
|
|
2022-10-14 19:51:44 +03:00
|
|
|
self._bg_canvas.configure(width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
|
|
|
self._canvas.configure(width=self._apply_widget_scaling(self._radiobutton_width),
|
|
|
|
height=self._apply_widget_scaling(self._radiobutton_height))
|
2022-10-15 02:02:54 +03:00
|
|
|
self._draw(no_color_updates=True)
|
2022-05-02 00:29:14 +03:00
|
|
|
|
2022-10-14 19:51:44 +03:00
|
|
|
def _set_dimensions(self, width: int = None, height: int = None):
|
|
|
|
super()._set_dimensions(width, height)
|
|
|
|
|
|
|
|
self._bg_canvas.configure(width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
|
|
|
|
2022-10-22 16:08:13 +03:00
|
|
|
def _update_font(self):
|
|
|
|
""" pass font to tkinter widgets with applied font scaling and update grid with workaround """
|
|
|
|
self._text_label.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._bg_canvas.grid_forget()
|
|
|
|
self._bg_canvas.grid(row=0, column=0, columnspan=3, sticky="nswe")
|
|
|
|
|
2022-03-05 04:01:07 +03:00
|
|
|
def destroy(self):
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._variable is not None:
|
|
|
|
self._variable.trace_remove("write", self._variable_callback_name)
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-10-22 16:08:13 +03:00
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.remove_size_configure_callback(self._update_font)
|
|
|
|
|
2022-03-05 04:01:07 +03:00
|
|
|
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-11-06 16:40:15 +03:00
|
|
|
if self._check_state is True:
|
|
|
|
requires_recoloring = self._draw_engine.draw_rounded_rect_with_border(self._apply_widget_scaling(self._radiobutton_width),
|
|
|
|
self._apply_widget_scaling(self._radiobutton_height),
|
|
|
|
self._apply_widget_scaling(self._corner_radius),
|
|
|
|
self._apply_widget_scaling(self._border_width_checked))
|
|
|
|
else:
|
|
|
|
requires_recoloring = self._draw_engine.draw_rounded_rect_with_border(self._apply_widget_scaling(self._radiobutton_width),
|
|
|
|
self._apply_widget_scaling(self._radiobutton_height),
|
|
|
|
self._apply_widget_scaling(self._corner_radius),
|
|
|
|
self._apply_widget_scaling(self._border_width_unchecked))
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-10-14 19:51:44 +03:00
|
|
|
if no_color_updates is False or requires_recoloring:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._bg_canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
|
|
|
|
self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-10-14 19:51:44 +03:00
|
|
|
if self._check_state is False:
|
|
|
|
self._canvas.itemconfig("border_parts",
|
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-14 19:51:44 +03:00
|
|
|
else:
|
|
|
|
self._canvas.itemconfig("border_parts",
|
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-03-05 04:01:07 +03:00
|
|
|
|
2022-10-14 19:51:44 +03:00
|
|
|
self._canvas.itemconfig("inner_parts",
|
2022-10-29 02:20:32 +03:00
|
|
|
outline=self._apply_appearance_mode(self._bg_color),
|
|
|
|
fill=self._apply_appearance_mode(self._bg_color))
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-10-14 19:51:44 +03:00
|
|
|
if self._state == tkinter.DISABLED:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._text_label.configure(fg=self._apply_appearance_mode(self._text_color_disabled))
|
2022-10-14 19:51:44 +03:00
|
|
|
else:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._text_label.configure(fg=self._apply_appearance_mode(self._text_color))
|
2022-03-10 04:12:30 +03:00
|
|
|
|
2022-10-29 02:20:32 +03:00
|
|
|
self._text_label.configure(bg=self._apply_appearance_mode(self._bg_color))
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
def configure(self, require_redraw=False, **kwargs):
|
2022-11-06 16:40:15 +03:00
|
|
|
if "corner_radius" in kwargs:
|
|
|
|
self._corner_radius = kwargs.pop("corner_radius")
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "border_width_unchecked" in kwargs:
|
|
|
|
self._border_width_unchecked = kwargs.pop("border_width_unchecked")
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "border_width_checked" in kwargs:
|
|
|
|
self._border_width_checked = kwargs.pop("border_width_checked")
|
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-14 19:51:44 +03:00
|
|
|
if "radiobutton_width" in kwargs:
|
|
|
|
self._radiobutton_width = kwargs.pop("radiobutton_width")
|
|
|
|
self._canvas.configure(width=self._apply_widget_scaling(self._radiobutton_width))
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "radiobutton_height" in kwargs:
|
|
|
|
self._radiobutton_height = kwargs.pop("radiobutton_height")
|
|
|
|
self._canvas.configure(height=self._apply_widget_scaling(self._radiobutton_height))
|
|
|
|
require_redraw = True
|
|
|
|
|
2022-03-05 04:01:07 +03:00
|
|
|
if "text" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text = kwargs.pop("text")
|
|
|
|
self._text_label.configure(text=self._text)
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
if "font" in kwargs:
|
2022-10-22 16:08:13 +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-03-05 04:01:07 +03:00
|
|
|
if "state" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._state = kwargs.pop("state")
|
|
|
|
self._set_cursor()
|
2022-03-10 04:12:30 +03:00
|
|
|
require_redraw = True
|
2022-03-05 04:01:07 +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-03-05 04:01:07 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "hover_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._hover_color = self._check_color_type(kwargs.pop("hover_color"))
|
2022-03-05 04:01:07 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "text_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._text_color = self._check_color_type(kwargs.pop("text_color"))
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "text_color_disabled" in kwargs:
|
|
|
|
self._text_color_disabled = self._check_color_type(kwargs.pop("text_color_disabled"))
|
2022-03-05 04:01:07 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
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-03-05 04:01:07 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-22 15:24:04 +03:00
|
|
|
if "hover" in kwargs:
|
|
|
|
self._hover = kwargs.pop("hover")
|
|
|
|
|
2022-03-05 04:01:07 +03:00
|
|
|
if "command" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._command = kwargs.pop("command")
|
2022-07-02 01:54:21 +03:00
|
|
|
|
|
|
|
if "textvariable" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._textvariable = kwargs.pop("textvariable")
|
|
|
|
self._text_label.configure(textvariable=self._textvariable)
|
2022-03-05 04:01:07 +03:00
|
|
|
|
|
|
|
if "variable" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._variable is not None:
|
|
|
|
self._variable.trace_remove("write", self._variable_callback_name)
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._variable = kwargs.pop("variable")
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._variable is not None and self._variable != "":
|
|
|
|
self._variable_callback_name = self._variable.trace_add("write", self._variable_callback)
|
|
|
|
self._check_state = True if self._variable.get() == self._value else False
|
2022-07-05 15:39:12 +03:00
|
|
|
require_redraw = True
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
super().configure(require_redraw=require_redraw, **kwargs)
|
2022-03-05 04:01:07 +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_unchecked":
|
|
|
|
return self._border_width_unchecked
|
|
|
|
elif attribute_name == "border_width_checked":
|
|
|
|
return self._border_width_checked
|
2022-10-14 19:51:44 +03:00
|
|
|
elif attribute_name == "radiobutton_width":
|
|
|
|
return self._radiobutton_width
|
|
|
|
elif attribute_name == "radiobutton_height":
|
|
|
|
return self._radiobutton_height
|
2022-10-03 01:33:06 +03:00
|
|
|
|
|
|
|
elif attribute_name == "fg_color":
|
|
|
|
return self._fg_color
|
|
|
|
elif attribute_name == "hover_color":
|
|
|
|
return self._hover_color
|
|
|
|
elif attribute_name == "border_color":
|
|
|
|
return self._border_color
|
|
|
|
elif attribute_name == "text_color":
|
|
|
|
return self._text_color
|
|
|
|
elif attribute_name == "text_color_disabled":
|
|
|
|
return self._text_color_disabled
|
|
|
|
|
|
|
|
elif attribute_name == "text":
|
|
|
|
return self._text
|
|
|
|
elif attribute_name == "font":
|
|
|
|
return self._font
|
|
|
|
elif attribute_name == "textvariable":
|
|
|
|
return self._textvariable
|
|
|
|
elif attribute_name == "variable":
|
|
|
|
return self._variable
|
|
|
|
elif attribute_name == "value":
|
|
|
|
return self._value
|
|
|
|
elif attribute_name == "state":
|
|
|
|
return self._state
|
|
|
|
elif attribute_name == "hover":
|
|
|
|
return self._hover
|
|
|
|
elif attribute_name == "command":
|
|
|
|
return self._command
|
2022-10-04 04:03:43 +03:00
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
else:
|
|
|
|
return super().cget(attribute_name)
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _set_cursor(self):
|
2022-10-22 15:24:04 +03:00
|
|
|
if self._cursor_manipulation_enabled:
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._state == tkinter.DISABLED:
|
2022-10-22 15:24:04 +03:00
|
|
|
if sys.platform == "darwin":
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(cursor="arrow")
|
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.configure(cursor="arrow")
|
2022-10-22 15:24:04 +03:00
|
|
|
elif sys.platform.startswith("win"):
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(cursor="arrow")
|
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.configure(cursor="arrow")
|
2022-05-02 00:29:14 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
elif self._state == tkinter.NORMAL:
|
2022-10-22 15:24:04 +03:00
|
|
|
if sys.platform == "darwin":
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(cursor="pointinghand")
|
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.configure(cursor="pointinghand")
|
2022-10-22 15:24:04 +03:00
|
|
|
elif sys.platform.startswith("win"):
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(cursor="hand2")
|
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.configure(cursor="hand2")
|
|
|
|
|
|
|
|
def _on_enter(self, event=0):
|
|
|
|
if self._hover is True and self._state == tkinter.NORMAL:
|
|
|
|
self._canvas.itemconfig("border_parts",
|
2022-10-29 02:20:32 +03:00
|
|
|
fill=self._apply_appearance_mode(self._hover_color),
|
|
|
|
outline=self._apply_appearance_mode(self._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 self._check_state is True:
|
|
|
|
self._canvas.itemconfig("border_parts",
|
2022-10-29 02:20:32 +03:00
|
|
|
fill=self._apply_appearance_mode(self._fg_color),
|
|
|
|
outline=self._apply_appearance_mode(self._fg_color))
|
2022-10-22 15:24:04 +03:00
|
|
|
else:
|
|
|
|
self._canvas.itemconfig("border_parts",
|
2022-10-29 02:20:32 +03:00
|
|
|
fill=self._apply_appearance_mode(self._border_color),
|
|
|
|
outline=self._apply_appearance_mode(self._border_color))
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _variable_callback(self, var_name, index, mode):
|
|
|
|
if not self._variable_callback_blocked:
|
|
|
|
if self._variable.get() == self._value:
|
2022-03-05 04:01:07 +03:00
|
|
|
self.select(from_variable_callback=True)
|
|
|
|
else:
|
|
|
|
self.deselect(from_variable_callback=True)
|
|
|
|
|
|
|
|
def invoke(self, event=0):
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._state == tkinter.NORMAL:
|
|
|
|
if self._check_state is False:
|
|
|
|
self._check_state = True
|
2022-03-05 04:01:07 +03:00
|
|
|
self.select()
|
|
|
|
|
2022-12-06 13:09:34 +03:00
|
|
|
if self._command is not None:
|
|
|
|
self._command()
|
2022-05-13 00:56:00 +03:00
|
|
|
|
2022-03-05 04:01:07 +03:00
|
|
|
def select(self, from_variable_callback=False):
|
2022-10-02 04:23:10 +03:00
|
|
|
self._check_state = True
|
|
|
|
self._draw()
|
2022-03-05 04:01:07 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._variable is not None and not from_variable_callback:
|
|
|
|
self._variable_callback_blocked = True
|
|
|
|
self._variable.set(self._value)
|
|
|
|
self._variable_callback_blocked = False
|
2022-03-05 04:01:07 +03:00
|
|
|
|
|
|
|
def deselect(self, from_variable_callback=False):
|
2022-10-02 04:23:10 +03:00
|
|
|
self._check_state = False
|
|
|
|
self._draw()
|
|
|
|
|
|
|
|
if self._variable is not None and not from_variable_callback:
|
|
|
|
self._variable_callback_blocked = True
|
|
|
|
self._variable.set("")
|
|
|
|
self._variable_callback_blocked = False
|
2022-10-04 03:26:29 +03:00
|
|
|
|
2022-12-06 20:09:20 +03:00
|
|
|
def bind(self, sequence: str = None, command: Callable = None, add: Union[str, bool] = True):
|
2022-10-04 04:03:43 +03:00
|
|
|
""" called on the tkinter.Canvas """
|
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._canvas.bind(sequence, command, add=True)
|
|
|
|
self._text_label.bind(sequence, command, add=True)
|
|
|
|
|
|
|
|
def unbind(self, sequence: str = None, funcid: str = None):
|
|
|
|
""" called on the tkinter.Label and tkinter.Canvas """
|
|
|
|
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._canvas.unbind(sequence, None)
|
|
|
|
self._text_label.unbind(sequence, None)
|
|
|
|
self._create_bindings(sequence=sequence) # restore internal callbacks for sequence
|
2022-10-05 22:11:46 +03:00
|
|
|
|
|
|
|
def focus(self):
|
|
|
|
return self._text_label.focus()
|
|
|
|
|
|
|
|
def focus_set(self):
|
|
|
|
return self._text_label.focus_set()
|
|
|
|
|
|
|
|
def focus_force(self):
|
|
|
|
return self._text_label.focus_force()
|