2022-11-11 01:27:55 +03:00
|
|
|
from typing import Union, Tuple, List, Optional
|
2022-10-02 04:23:10 +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
|
2022-04-20 23:50:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CTkFrame(CTkBaseClass):
|
2022-10-02 04:23:10 +03:00
|
|
|
"""
|
|
|
|
Frame with rounded corners and border.
|
|
|
|
Default foreground colors are set according to theme.
|
|
|
|
To make the frame transparent set fg_color=None.
|
|
|
|
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 = 200,
|
|
|
|
height: int = 200,
|
2022-11-11 01:27:55 +03:00
|
|
|
corner_radius: Optional[Union[int, str]] = None,
|
|
|
|
border_width: Optional[Union[int, str]] = 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,
|
|
|
|
border_color: Optional[Union[str, Tuple[str, str]]] = None,
|
2022-10-03 01:33:06 +03:00
|
|
|
|
2023-02-04 18:53:48 +03:00
|
|
|
background_corner_colors: Union[Tuple[Union[str, Tuple[str, str]]], None] = None,
|
2022-11-11 01:27:55 +03:00
|
|
|
overwrite_preferred_drawing_method: Union[str, None] = None,
|
2022-04-20 23:50:57 +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-04-20 23:50:57 +03:00
|
|
|
|
|
|
|
# color
|
2022-11-27 04:48:09 +03:00
|
|
|
self._border_color = ThemeManager.theme["CTkFrame"]["border_color"] if border_color is None else self._check_color_type(border_color)
|
2022-04-20 23:50:57 +03:00
|
|
|
|
2022-05-25 19:37:55 +03:00
|
|
|
# determine fg_color of frame
|
2022-11-11 01:27:55 +03:00
|
|
|
if fg_color is None:
|
2022-04-20 23:50:57 +03:00
|
|
|
if isinstance(self.master, CTkFrame):
|
2022-11-27 04:48:09 +03:00
|
|
|
if self.master._fg_color == ThemeManager.theme["CTkFrame"]["fg_color"]:
|
|
|
|
self._fg_color = ThemeManager.theme["CTkFrame"]["top_fg_color"]
|
2022-04-20 23:50:57 +03:00
|
|
|
else:
|
2022-11-27 04:48:09 +03:00
|
|
|
self._fg_color = ThemeManager.theme["CTkFrame"]["fg_color"]
|
2022-04-20 23:50:57 +03:00
|
|
|
else:
|
2022-11-27 04:48:09 +03:00
|
|
|
self._fg_color = ThemeManager.theme["CTkFrame"]["fg_color"]
|
2022-04-20 23:50:57 +03:00
|
|
|
else:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._fg_color = self._check_color_type(fg_color, transparency=True)
|
2022-04-20 23:50:57 +03:00
|
|
|
|
2022-10-10 01:48:08 +03:00
|
|
|
self._background_corner_colors = background_corner_colors # rendering options for DrawEngine
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# shape
|
2022-11-27 04:48:09 +03:00
|
|
|
self._corner_radius = ThemeManager.theme["CTkFrame"]["corner_radius"] if corner_radius is None else corner_radius
|
|
|
|
self._border_width = ThemeManager.theme["CTkFrame"]["border_width"] if border_width is None else border_width
|
2022-10-02 04:23:10 +03:00
|
|
|
|
|
|
|
self._canvas = CTkCanvas(master=self,
|
|
|
|
highlightthickness=0,
|
|
|
|
width=self._apply_widget_scaling(self._current_width),
|
|
|
|
height=self._apply_widget_scaling(self._current_height))
|
|
|
|
self._canvas.place(x=0, y=0, relwidth=1, relheight=1)
|
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._draw_engine = DrawEngine(self._canvas)
|
2022-05-25 19:37:55 +03:00
|
|
|
self._overwrite_preferred_drawing_method = overwrite_preferred_drawing_method
|
2022-04-20 23:50:57 +03:00
|
|
|
|
2022-10-15 02:02:54 +03:00
|
|
|
self._draw(no_color_updates=True)
|
2022-04-20 23:50:57 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def winfo_children(self) -> List[any]:
|
|
|
|
"""
|
|
|
|
winfo_children of CTkFrame without self.canvas widget,
|
|
|
|
because it's not a child but part of the CTkFrame itself
|
|
|
|
"""
|
2022-04-20 23:50:57 +03:00
|
|
|
|
|
|
|
child_widgets = super().winfo_children()
|
|
|
|
try:
|
2022-10-02 04:23:10 +03:00
|
|
|
child_widgets.remove(self._canvas)
|
2022-04-20 23:50:57 +03:00
|
|
|
return child_widgets
|
|
|
|
except ValueError:
|
|
|
|
return child_widgets
|
|
|
|
|
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-11-11 01:27:55 +03:00
|
|
|
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
2022-10-02 04:23:10 +03:00
|
|
|
self._draw()
|
2022-05-02 00:29:14 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _set_dimensions(self, width=None, height=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-24 02:00:58 +03:00
|
|
|
|
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-05 22:11:46 +03:00
|
|
|
if not self._canvas.winfo_exists():
|
|
|
|
return
|
|
|
|
|
2022-10-10 01:48:08 +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-10 01:48:08 +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),
|
|
|
|
overwrite_preferred_drawing_method=self._overwrite_preferred_drawing_method)
|
2022-04-20 23:50:57 +03:00
|
|
|
|
|
|
|
if no_color_updates is False or requires_recoloring:
|
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
|
|
|
fill=self._apply_appearance_mode(self._bg_color),
|
|
|
|
outline=self._apply_appearance_mode(self._bg_color))
|
2022-05-13 00:56:00 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("inner_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-05-13 00:56:00 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
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))
|
|
|
|
self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
|
2022-04-20 23:50:57 +03:00
|
|
|
|
2022-10-10 01:48:08 +03:00
|
|
|
# self._canvas.tag_lower("inner_parts") # maybe unnecessary, I don't know ???
|
|
|
|
# self._canvas.tag_lower("border_parts")
|
2022-04-20 23:50:57 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
def configure(self, require_redraw=False, **kwargs):
|
2022-04-20 23:50:57 +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"), transparency=True)
|
2022-04-20 23:50:57 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-10 01:48:08 +03:00
|
|
|
# check if CTk widgets are children of the frame and change their bg_color to new frame fg_color
|
2022-04-20 23:50:57 +03:00
|
|
|
for child in self.winfo_children():
|
|
|
|
if isinstance(child, CTkBaseClass):
|
2022-10-02 04:23:10 +03:00
|
|
|
child.configure(bg_color=self._fg_color)
|
2022-04-20 23:50:57 +03:00
|
|
|
|
2022-10-10 01:48:08 +03:00
|
|
|
if "bg_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
# pass bg_color change to children if fg_color is "transparent"
|
|
|
|
if self._fg_color == "transparent":
|
2022-10-10 01:48:08 +03:00
|
|
|
for child in self.winfo_children():
|
|
|
|
if isinstance(child, CTkBaseClass):
|
|
|
|
child.configure(bg_color=self._fg_color)
|
2022-10-08 02:50:09 +03:00
|
|
|
|
2022-05-22 22:05:46 +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-05-22 22:05:46 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-10-10 01:48:08 +03:00
|
|
|
if "background_corner_colors" in kwargs:
|
|
|
|
self._background_corner_colors = kwargs.pop("background_corner_colors")
|
|
|
|
require_redraw = True
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
if "corner_radius" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._corner_radius = kwargs.pop("corner_radius")
|
2022-04-20 23:50:57 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-05-22 22:05:46 +03:00
|
|
|
if "border_width" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._border_width = kwargs.pop("border_width")
|
2022-05-22 22:05:46 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
super().configure(require_redraw=require_redraw, **kwargs)
|
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
|
2022-10-10 01:48:08 +03:00
|
|
|
elif attribute_name == "background_corner_colors":
|
|
|
|
return self._background_corner_colors
|
2022-10-04 03:26:29 +03:00
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
else:
|
|
|
|
return super().cget(attribute_name)
|
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 03:26:29 +03:00
|
|
|
""" called on the tkinter.Canvas """
|
2022-12-06 20:09:20 +03:00
|
|
|
if not (add == "+" or add is True):
|
|
|
|
raise ValueError("'add' argument can only be '+' or True to preserve internal callbacks")
|
|
|
|
self._canvas.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.Canvas """
|
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._canvas.unbind(sequence, None)
|