2021-03-04 20:27:46 +03:00
|
|
|
import tkinter
|
|
|
|
import sys
|
2022-11-11 01:27:55 +03:00
|
|
|
from typing import Union, Tuple, Callable, Optional
|
2021-03-04 20:27:46 +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
|
|
|
|
from .image import CTkImage
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
class CTkButton(CTkBaseClass):
|
2022-10-02 04:23:10 +03:00
|
|
|
"""
|
|
|
|
Button with rounded corners, border, hover effect, image support, click command and textvariable.
|
|
|
|
For detailed information check out the documentation.
|
|
|
|
"""
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
_image_label_spacing: int = 6
|
2022-10-29 01:42:33 +03:00
|
|
|
|
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,
|
2022-10-29 01:42:33 +03:00
|
|
|
border_spacing: int = 2,
|
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
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
background_corner_colors: Union[Tuple[Union[str, Tuple[str, str]]], None] = None,
|
2022-10-08 02:50:09 +03:00
|
|
|
round_width_to_even_numbers: bool = True,
|
|
|
|
round_height_to_even_numbers: bool = True,
|
|
|
|
|
2022-07-07 17:21:30 +03:00
|
|
|
text: str = "CTkButton",
|
2022-11-11 01:27:55 +03:00
|
|
|
font: Optional[Union[tuple, CTkFont]] = None,
|
|
|
|
textvariable: Union[tkinter.Variable, None] = None,
|
|
|
|
image: Union[tkinter.PhotoImage, CTkImage, None] = None,
|
2022-07-07 17:21:30 +03:00
|
|
|
state: str = "normal",
|
2022-10-03 01:33:06 +03:00
|
|
|
hover: bool = True,
|
2022-11-11 01:27:55 +03:00
|
|
|
command: Union[Callable[[], None], None] = None,
|
2022-10-03 01:33:06 +03:00
|
|
|
compound: str = "left",
|
2022-10-29 01:42:33 +03:00
|
|
|
anchor: str = "center",
|
2022-01-16 18:54:21 +03:00
|
|
|
**kwargs):
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-11-11 01:27:55 +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)
|
2021-09-13 15:27:29 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
# shape
|
2022-11-27 04:48:09 +03:00
|
|
|
self._corner_radius: int = ThemeManager.theme["CTkButton"]["corner_radius"] if corner_radius is None else corner_radius
|
2022-11-11 01:27:55 +03:00
|
|
|
self._corner_radius = min(self._corner_radius, round(self._current_height / 2))
|
2022-11-27 04:48:09 +03:00
|
|
|
self._border_width: int = ThemeManager.theme["CTkButton"]["border_width"] if border_width is None else border_width
|
2022-11-11 01:27:55 +03:00
|
|
|
self._border_spacing: int = border_spacing
|
|
|
|
|
2022-07-07 17:21:30 +03:00
|
|
|
# color
|
2022-11-27 04:48:09 +03:00
|
|
|
self._fg_color: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["fg_color"] if fg_color is None else self._check_color_type(fg_color, transparency=True)
|
|
|
|
self._hover_color: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["hover_color"] if hover_color is None else self._check_color_type(hover_color)
|
|
|
|
self._border_color: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["border_color"] if border_color is None else self._check_color_type(border_color)
|
|
|
|
self._text_color: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["text_color"] if text_color is None else self._check_color_type(text_color)
|
|
|
|
self._text_color_disabled: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["text_color_disabled"] if text_color_disabled is None else self._check_color_type(text_color_disabled)
|
2022-11-11 01:27:55 +03:00
|
|
|
|
|
|
|
# rendering options
|
|
|
|
self._background_corner_colors: Union[Tuple[Union[str, Tuple[str, str]]], None] = background_corner_colors # rendering options for DrawEngine
|
|
|
|
self._round_width_to_even_numbers: bool = round_width_to_even_numbers # rendering options for DrawEngine
|
|
|
|
self._round_height_to_even_numbers: bool = round_height_to_even_numbers # rendering options for DrawEngine
|
|
|
|
|
|
|
|
# text, font
|
|
|
|
self._text = text
|
|
|
|
self._text_label: Union[tkinter.Label, None] = None
|
|
|
|
self._textvariable: tkinter.Variable = textvariable
|
|
|
|
self._font: Union[tuple, CTkFont] = CTkFont() if font is None else self._check_font_type(font)
|
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.add_size_configure_callback(self._update_font)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
# image
|
2022-11-06 16:40:15 +03:00
|
|
|
self._image = self._check_image_type(image)
|
2022-10-05 22:11:46 +03:00
|
|
|
self._image_label: Union[tkinter.Label, None] = None
|
2022-11-01 02:37:30 +03:00
|
|
|
if isinstance(self._image, CTkImage):
|
|
|
|
self._image.add_configure_callback(self._update_image)
|
2022-10-21 22:28:31 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
# other
|
|
|
|
self._state: str = state
|
|
|
|
self._hover: bool = hover
|
|
|
|
self._command: Callable = command
|
|
|
|
self._compound: str = compound
|
|
|
|
self._anchor: str = anchor
|
2022-10-05 22:11:46 +03:00
|
|
|
self._click_animation_running: bool = False
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
# canvas and draw engine
|
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))
|
2022-10-29 01:42:33 +03:00
|
|
|
self._canvas.grid(row=0, column=0, rowspan=5, columnspan=5, sticky="nsew")
|
2022-10-02 04:23:10 +03:00
|
|
|
self._draw_engine = DrawEngine(self._canvas)
|
2022-10-08 02:50:09 +03:00
|
|
|
self._draw_engine.set_round_to_even_numbers(self._round_width_to_even_numbers, self._round_height_to_even_numbers) # rendering options
|
2022-02-24 02:19:40 +03:00
|
|
|
|
2022-07-07 17:21:30 +03:00
|
|
|
# configure cursor and initial draw
|
2022-12-06 13:09:34 +03:00
|
|
|
self._create_bindings()
|
2022-10-02 04:23:10 +03:00
|
|
|
self._set_cursor()
|
|
|
|
self._draw()
|
2021-11-04 18:20:08 +03:00
|
|
|
|
2022-12-06 13:09:34 +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 or sequence == "<Enter>":
|
|
|
|
self._canvas.bind("<Enter>", self._on_enter)
|
|
|
|
if sequence is None or sequence == "<Leave>":
|
|
|
|
self._canvas.bind("<Leave>", self._on_leave)
|
|
|
|
if sequence is None or sequence == "<Button-1>":
|
|
|
|
self._canvas.bind("<Button-1>", self._clicked)
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _set_scaling(self, *args, **kwargs):
|
|
|
|
super()._set_scaling(*args, **kwargs)
|
2022-05-02 00:29:14 +03:00
|
|
|
|
2022-10-29 01:42:33 +03:00
|
|
|
self._create_grid()
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._text_label is not None:
|
2022-10-15 02:02:54 +03:00
|
|
|
self._text_label.configure(font=self._apply_font_scaling(self._font))
|
2022-05-02 00:29:14 +03:00
|
|
|
|
2022-11-30 23:31:40 +03:00
|
|
|
self._update_image()
|
2022-11-11 01:27:55 +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-24 02:00:58 +03:00
|
|
|
|
2022-11-30 23:31:40 +03:00
|
|
|
def _set_appearance_mode(self, mode_string):
|
|
|
|
super()._set_appearance_mode(mode_string)
|
|
|
|
self._update_image()
|
|
|
|
|
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-24 02:00:58 +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-02 00:29:14 +03:00
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
def _update_font(self):
|
|
|
|
""" pass font to tkinter widgets with applied font scaling and update grid with workaround """
|
|
|
|
if self._text_label is not None:
|
|
|
|
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._canvas.grid_forget()
|
2022-10-29 01:42:33 +03:00
|
|
|
self._canvas.grid(row=0, column=0, rowspan=5, columnspan=5, sticky="nsew")
|
2022-10-21 22:28:31 +03:00
|
|
|
|
2022-11-01 02:37:30 +03:00
|
|
|
def _update_image(self):
|
|
|
|
if self._image_label is not None:
|
|
|
|
self._image_label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),
|
|
|
|
self._get_appearance_mode()))
|
|
|
|
|
2022-10-21 22:28:31 +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-10-08 02:50:09 +03:00
|
|
|
if self._background_corner_colors is not None:
|
|
|
|
self._draw_engine.draw_background_corners(self._apply_widget_scaling(self._current_width),
|
|
|
|
self._apply_widget_scaling(self._current_height))
|
2022-10-29 02:20:32 +03:00
|
|
|
self._canvas.itemconfig("background_corner_top_left", fill=self._apply_appearance_mode(self._background_corner_colors[0]))
|
|
|
|
self._canvas.itemconfig("background_corner_top_right", fill=self._apply_appearance_mode(self._background_corner_colors[1]))
|
|
|
|
self._canvas.itemconfig("background_corner_bottom_right", fill=self._apply_appearance_mode(self._background_corner_colors[2]))
|
|
|
|
self._canvas.itemconfig("background_corner_bottom_left", fill=self._apply_appearance_mode(self._background_corner_colors[3]))
|
2022-10-08 02:50:09 +03:00
|
|
|
else:
|
|
|
|
self._canvas.delete("background_parts")
|
|
|
|
|
2022-10-02 04:23:10 +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),
|
|
|
|
self._apply_widget_scaling(self._corner_radius),
|
|
|
|
self._apply_widget_scaling(self._border_width))
|
2022-02-24 02:19:40 +03:00
|
|
|
|
|
|
|
if no_color_updates is False or requires_recoloring:
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2022-10-29 02:20:32 +03:00
|
|
|
self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
|
2021-11-07 17:12:09 +03:00
|
|
|
|
2021-11-10 02:03:08 +03:00
|
|
|
# set color for the button border parts (outline)
|
2022-10-02 04:23:10 +03:00
|
|
|
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))
|
2021-11-10 02:03:08 +03:00
|
|
|
|
2022-03-10 04:12:30 +03:00
|
|
|
# set color for inner button parts
|
2022-11-11 01:27:55 +03:00
|
|
|
if self._fg_color == "transparent":
|
2022-10-02 04:23:10 +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))
|
2021-11-10 02:03:08 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("inner_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))
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
# create text label if text given
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._text is not None and self._text != "":
|
2021-11-07 17:12:09 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._text_label is None:
|
|
|
|
self._text_label = tkinter.Label(master=self,
|
2022-10-03 01:33:06 +03:00
|
|
|
font=self._apply_font_scaling(self._font),
|
2022-10-02 04:23:10 +03:00
|
|
|
text=self._text,
|
2022-10-13 18:28:33 +03:00
|
|
|
padx=0,
|
|
|
|
pady=0,
|
|
|
|
borderwidth=1,
|
2022-10-02 04:23:10 +03:00
|
|
|
textvariable=self._textvariable)
|
2022-10-29 01:42:33 +03:00
|
|
|
self._create_grid()
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text_label.bind("<Enter>", self._on_enter)
|
|
|
|
self._text_label.bind("<Leave>", self._on_leave)
|
|
|
|
self._text_label.bind("<Button-1>", self._clicked)
|
|
|
|
self._text_label.bind("<Button-1>", self._clicked)
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
if no_color_updates is False:
|
2021-11-07 02:01:17 +03:00
|
|
|
# set text_label fg color (text color)
|
2022-10-29 02:20:32 +03:00
|
|
|
self._text_label.configure(fg=self._apply_appearance_mode(self._text_color))
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2022-10-02 04:23:10 +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)))
|
2021-11-07 02:01:17 +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-11-11 01:27:55 +03:00
|
|
|
if self._apply_appearance_mode(self._fg_color) == "transparent":
|
2022-10-29 02:20:32 +03:00
|
|
|
self._text_label.configure(bg=self._apply_appearance_mode(self._bg_color))
|
2022-03-10 04:12:30 +03:00
|
|
|
else:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._text_label.configure(bg=self._apply_appearance_mode(self._fg_color))
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
else:
|
|
|
|
# delete text_label if no text given
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.destroy()
|
|
|
|
self._text_label = None
|
2022-10-29 01:42:33 +03:00
|
|
|
self._create_grid()
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
# create image label if image given
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._image is not None:
|
2021-11-07 17:12:09 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._image_label is None:
|
|
|
|
self._image_label = tkinter.Label(master=self)
|
2022-11-11 01:27:55 +03:00
|
|
|
self._update_image() # set image
|
2022-10-29 01:42:33 +03:00
|
|
|
self._create_grid()
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._image_label.bind("<Enter>", self._on_enter)
|
|
|
|
self._image_label.bind("<Leave>", self._on_leave)
|
|
|
|
self._image_label.bind("<Button-1>", self._clicked)
|
|
|
|
self._image_label.bind("<Button-1>", self._clicked)
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
if no_color_updates is False:
|
2021-11-07 02:01:17 +03:00
|
|
|
# set image_label bg color (background color of label)
|
2022-11-11 01:27:55 +03:00
|
|
|
if self._apply_appearance_mode(self._fg_color) == "transparent":
|
2022-10-29 02:20:32 +03:00
|
|
|
self._image_label.configure(bg=self._apply_appearance_mode(self._bg_color))
|
2021-11-07 02:01:17 +03:00
|
|
|
else:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._image_label.configure(bg=self._apply_appearance_mode(self._fg_color))
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
else:
|
|
|
|
# delete text_label if no text given
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._image_label is not None:
|
|
|
|
self._image_label.destroy()
|
|
|
|
self._image_label = None
|
2022-10-29 01:42:33 +03:00
|
|
|
self._create_grid()
|
|
|
|
|
|
|
|
def _create_grid(self):
|
|
|
|
""" configure grid system (5x5) """
|
|
|
|
|
|
|
|
# Outer rows and columns have weight of 1000 to overpower the rows and columns of the label and image with weight 1.
|
|
|
|
# Rows and columns of image and label need weight of 1 to collapse in case of missing space on the button,
|
|
|
|
# so image and label need sticky option to stick together in the center, and therefore outer rows and columns
|
|
|
|
# need weight of 100 in case of other anchor than center.
|
|
|
|
n_padding_weight, s_padding_weight, e_padding_weight, w_padding_weight = 1000, 1000, 1000, 1000
|
|
|
|
if self._anchor != "center":
|
|
|
|
if "n" in self._anchor:
|
|
|
|
n_padding_weight, s_padding_weight = 0, 1000
|
|
|
|
if "s" in self._anchor:
|
|
|
|
n_padding_weight, s_padding_weight = 1000, 0
|
|
|
|
if "e" in self._anchor:
|
|
|
|
e_padding_weight, w_padding_weight = 1000, 0
|
|
|
|
if "w" in self._anchor:
|
|
|
|
e_padding_weight, w_padding_weight = 0, 1000
|
|
|
|
|
|
|
|
scaled_minsize_rows = self._apply_widget_scaling(max(self._border_width + 1, self._border_spacing))
|
|
|
|
scaled_minsize_columns = self._apply_widget_scaling(max(self._corner_radius, self._border_width + 1, self._border_spacing))
|
|
|
|
|
|
|
|
self.grid_rowconfigure(0, weight=n_padding_weight, minsize=scaled_minsize_rows)
|
|
|
|
self.grid_rowconfigure(4, weight=s_padding_weight, minsize=scaled_minsize_rows)
|
|
|
|
self.grid_columnconfigure(0, weight=e_padding_weight, minsize=scaled_minsize_columns)
|
|
|
|
self.grid_columnconfigure(4, weight=w_padding_weight, minsize=scaled_minsize_columns)
|
|
|
|
|
|
|
|
if self._compound in ("right", "left"):
|
|
|
|
self.grid_rowconfigure(2, weight=1)
|
|
|
|
if self._image_label is not None and self._text_label is not None:
|
|
|
|
self.grid_columnconfigure(2, weight=0, minsize=self._apply_widget_scaling(self._image_label_spacing))
|
|
|
|
else:
|
|
|
|
self.grid_columnconfigure(2, weight=0)
|
|
|
|
|
|
|
|
self.grid_rowconfigure((1, 3), weight=0)
|
|
|
|
self.grid_columnconfigure((1, 3), weight=1)
|
|
|
|
else:
|
|
|
|
self.grid_columnconfigure(2, weight=1)
|
|
|
|
if self._image_label is not None and self._text_label is not None:
|
|
|
|
self.grid_rowconfigure(2, weight=0, minsize=self._apply_widget_scaling(self._image_label_spacing))
|
|
|
|
else:
|
|
|
|
self.grid_rowconfigure(2, weight=0)
|
2021-11-07 17:12:09 +03:00
|
|
|
|
2022-10-29 01:42:33 +03:00
|
|
|
self.grid_columnconfigure((1, 3), weight=0)
|
|
|
|
self.grid_rowconfigure((1, 3), weight=1)
|
|
|
|
|
|
|
|
if self._compound == "right":
|
|
|
|
if self._image_label is not None:
|
|
|
|
self._image_label.grid(row=2, column=3, sticky="w")
|
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.grid(row=2, column=1, sticky="e")
|
|
|
|
elif self._compound == "left":
|
|
|
|
if self._image_label is not None:
|
|
|
|
self._image_label.grid(row=2, column=1, sticky="e")
|
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.grid(row=2, column=3, sticky="w")
|
|
|
|
elif self._compound == "top":
|
|
|
|
if self._image_label is not None:
|
|
|
|
self._image_label.grid(row=1, column=2, sticky="s")
|
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.grid(row=3, column=2, sticky="n")
|
|
|
|
elif self._compound == "bottom":
|
|
|
|
if self._image_label is not None:
|
|
|
|
self._image_label.grid(row=3, column=2, sticky="n")
|
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.grid(row=1, column=2, sticky="s")
|
2022-05-04 16:38:13 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
def configure(self, require_redraw=False, **kwargs):
|
2022-10-22 16:24:15 +03:00
|
|
|
if "corner_radius" in kwargs:
|
|
|
|
self._corner_radius = kwargs.pop("corner_radius")
|
2022-10-29 01:42:33 +03:00
|
|
|
self._create_grid()
|
2022-10-22 16:24:15 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "border_width" in kwargs:
|
|
|
|
self._border_width = kwargs.pop("border_width")
|
2022-10-29 01:42:33 +03:00
|
|
|
self._create_grid()
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "border_spacing" in kwargs:
|
|
|
|
self._border_spacing = kwargs.pop("border_spacing")
|
|
|
|
self._create_grid()
|
2022-10-22 16:24:15 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "fg_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._fg_color = self._check_color_type(kwargs.pop("fg_color"), transparency=True)
|
2022-10-22 16:24:15 +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-10-22 16:24:15 +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-10-22 16:24:15 +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"))
|
2022-10-22 16:24:15 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
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-22 16:24:15 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "background_corner_colors" in kwargs:
|
|
|
|
self._background_corner_colors = kwargs.pop("background_corner_colors")
|
|
|
|
require_redraw = True
|
|
|
|
|
2021-07-23 01:44:13 +03:00
|
|
|
if "text" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text = kwargs.pop("text")
|
|
|
|
if self._text_label is None:
|
2022-07-07 17:21:30 +03:00
|
|
|
require_redraw = True # text_label will be created in .draw()
|
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text_label.configure(text=self._text)
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
if "font" in kwargs:
|
2022-10-21 22:28:31 +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)
|
|
|
|
|
2022-10-22 16:08:13 +03:00
|
|
|
self._update_font()
|
2022-08-16 15:05:15 +03:00
|
|
|
|
2022-10-22 16:24:15 +03:00
|
|
|
if "textvariable" in kwargs:
|
|
|
|
self._textvariable = kwargs.pop("textvariable")
|
|
|
|
if self._text_label is not None:
|
|
|
|
self._text_label.configure(textvariable=self._textvariable)
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
if "image" in kwargs:
|
2022-11-01 02:37:30 +03:00
|
|
|
if isinstance(self._image, CTkImage):
|
|
|
|
self._image.remove_configure_callback(self._update_image)
|
2022-11-06 16:40:15 +03:00
|
|
|
self._image = self._check_image_type(kwargs.pop("image"))
|
2022-11-01 02:37:30 +03:00
|
|
|
if isinstance(self._image, CTkImage):
|
|
|
|
self._image.add_configure_callback(self._update_image)
|
2022-07-12 22:52:52 +03:00
|
|
|
require_redraw = True
|
2021-11-07 00:06:27 +03:00
|
|
|
|
2022-10-22 16:24:15 +03:00
|
|
|
if "state" in kwargs:
|
|
|
|
self._state = kwargs.pop("state")
|
|
|
|
self._set_cursor()
|
2021-11-07 00:06:27 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-22 15:24:04 +03:00
|
|
|
if "hover" in kwargs:
|
|
|
|
self._hover = kwargs.pop("hover")
|
|
|
|
|
2022-01-08 15:13:10 +03:00
|
|
|
if "command" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._command = kwargs.pop("command")
|
2022-01-08 15:13:10 +03:00
|
|
|
|
2022-10-22 16:24:15 +03:00
|
|
|
if "compound" in kwargs:
|
|
|
|
self._compound = kwargs.pop("compound")
|
2022-10-08 02:50:09 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-29 01:42:33 +03:00
|
|
|
if "anchor" in kwargs:
|
|
|
|
self._anchor = kwargs.pop("anchor")
|
|
|
|
require_redraw = True
|
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
super().configure(require_redraw=require_redraw, **kwargs)
|
2021-11-07 00:06:27 +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
|
2022-10-29 01:42:33 +03:00
|
|
|
elif attribute_name == "border_spacing":
|
|
|
|
return self._border_spacing
|
2022-10-03 01:33:06 +03:00
|
|
|
|
|
|
|
elif attribute_name == "fg_color":
|
2022-10-02 04:53:27 +03:00
|
|
|
return self._fg_color
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "hover_color":
|
2022-10-02 04:53:27 +03:00
|
|
|
return self._hover_color
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "border_color":
|
2022-10-02 04:53:27 +03:00
|
|
|
return self._border_color
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "text_color":
|
2022-10-02 04:53:27 +03:00
|
|
|
return self._text_color
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "text_color_disabled":
|
2022-10-02 04:53:27 +03:00
|
|
|
return self._text_color_disabled
|
2022-10-08 02:50:09 +03:00
|
|
|
elif attribute_name == "background_corner_colors":
|
|
|
|
return self._background_corner_colors
|
2022-10-03 01:33:06 +03:00
|
|
|
|
|
|
|
elif attribute_name == "text":
|
2022-10-02 04:53:27 +03:00
|
|
|
return self._text
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "font":
|
|
|
|
return self._font
|
|
|
|
elif attribute_name == "textvariable":
|
2022-10-02 04:53:27 +03:00
|
|
|
return self._textvariable
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "image":
|
2022-10-02 04:53:27 +03:00
|
|
|
return self._image
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "state":
|
2022-10-02 04:53:27 +03:00
|
|
|
return self._state
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "hover":
|
|
|
|
return self._hover
|
|
|
|
elif attribute_name == "command":
|
|
|
|
return self._command
|
|
|
|
elif attribute_name == "compound":
|
|
|
|
return self._compound
|
2022-10-29 01:42:33 +03:00
|
|
|
elif attribute_name == "anchor":
|
|
|
|
return self._anchor
|
2022-10-02 04:53:27 +03:00
|
|
|
else:
|
2022-10-03 01:33:06 +03:00
|
|
|
return super().cget(attribute_name)
|
2022-10-02 04:53:27 +03:00
|
|
|
|
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" and self._command is not None:
|
2022-05-02 00:29:14 +03:00
|
|
|
self.configure(cursor="arrow")
|
2022-10-22 15:24:04 +03:00
|
|
|
elif sys.platform.startswith("win") and self._command is not None:
|
2022-05-02 00:29:14 +03:00
|
|
|
self.configure(cursor="arrow")
|
|
|
|
|
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" and self._command is not None:
|
2022-05-02 00:29:14 +03:00
|
|
|
self.configure(cursor="pointinghand")
|
2022-10-22 15:24:04 +03:00
|
|
|
elif sys.platform.startswith("win") and self._command is not None:
|
2022-05-02 00:29:14 +03:00
|
|
|
self.configure(cursor="hand2")
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _on_enter(self, event=None):
|
2022-10-22 15:24:04 +03:00
|
|
|
if self._hover is True and self._state == "normal":
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._hover_color is None:
|
|
|
|
inner_parts_color = self._fg_color
|
2022-01-08 02:29:53 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
inner_parts_color = self._hover_color
|
2022-01-08 02:29:53 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
# set color of inner button parts to hover color
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("inner_parts",
|
2022-10-29 02:20:32 +03:00
|
|
|
outline=self._apply_appearance_mode(inner_parts_color),
|
|
|
|
fill=self._apply_appearance_mode(inner_parts_color))
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
# set text_label bg color to button hover color
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._text_label is not None:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._text_label.configure(bg=self._apply_appearance_mode(inner_parts_color))
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
# set image_label bg color to button hover color
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._image_label is not None:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._image_label.configure(bg=self._apply_appearance_mode(inner_parts_color))
|
2021-03-08 20:17:36 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _on_leave(self, event=None):
|
|
|
|
self._click_animation_running = False
|
2022-01-18 01:23:47 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
if self._fg_color == "transparent":
|
2022-10-22 15:24:04 +03:00
|
|
|
inner_parts_color = self._bg_color
|
|
|
|
else:
|
|
|
|
inner_parts_color = self._fg_color
|
2022-01-07 22:54:45 +03:00
|
|
|
|
2022-10-22 15:24:04 +03:00
|
|
|
# set color of inner button parts
|
|
|
|
self._canvas.itemconfig("inner_parts",
|
2022-10-29 02:20:32 +03:00
|
|
|
outline=self._apply_appearance_mode(inner_parts_color),
|
|
|
|
fill=self._apply_appearance_mode(inner_parts_color))
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2022-10-22 15:24:04 +03:00
|
|
|
# set text_label bg color (label color)
|
|
|
|
if self._text_label is not None:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._text_label.configure(bg=self._apply_appearance_mode(inner_parts_color))
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2022-10-22 15:24:04 +03:00
|
|
|
# set image_label bg color (image bg color)
|
|
|
|
if self._image_label is not None:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._image_label.configure(bg=self._apply_appearance_mode(inner_parts_color))
|
2021-03-08 20:17:36 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _click_animation(self):
|
|
|
|
if self._click_animation_running:
|
|
|
|
self._on_enter()
|
2022-01-18 01:23:47 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _clicked(self, event=None):
|
2022-11-11 02:32:52 +03:00
|
|
|
if self._state != tkinter.DISABLED:
|
2022-01-18 01:23:47 +03:00
|
|
|
|
2022-11-11 02:32:52 +03:00
|
|
|
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()
|
|
|
|
self._on_leave()
|
|
|
|
self._click_animation_running = True
|
|
|
|
self.after(100, self._click_animation)
|
2022-01-18 01:23:47 +03:00
|
|
|
|
2022-11-11 02:32:52 +03:00
|
|
|
if self._command is not None:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._command()
|
2022-10-04 03:26:29 +03:00
|
|
|
|
2022-11-11 02:32:52 +03:00
|
|
|
def invoke(self):
|
|
|
|
""" calls command function if button is not disabled """
|
|
|
|
if self._state != tkinter.DISABLED:
|
|
|
|
if self._command is not None:
|
|
|
|
return self._command()
|
|
|
|
|
2022-12-06 20:09:20 +03:00
|
|
|
def bind(self, sequence: str = None, command: Callable = None, add: Union[str, bool] = True):
|
2022-12-06 13:09:34 +03:00
|
|
|
""" called on the tkinter.Canvas """
|
|
|
|
if add != "+" or add is not True:
|
|
|
|
raise ValueError("'add' argument can only be '+' or True to preserve internal callbacks")
|
2022-12-06 20:09:20 +03:00
|
|
|
self._canvas.bind(sequence, command, add=True)
|
|
|
|
self._text_label.bind(sequence, command, add=True)
|
2022-10-04 17:55:34 +03:00
|
|
|
|
2022-12-06 20:09:20 +03:00
|
|
|
def unbind(self, sequence: str = None, funcid: str = None):
|
2022-10-04 17:55:34 +03:00
|
|
|
""" called on the tkinter.Label and tkinter.Canvas """
|
2022-12-06 13:09:34 +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._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()
|