Skip to content

Commit

Permalink
fixed some configure bugs TomSchimansky#585 TomSchimansky#584 TomSchi…
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSchimansky committed Nov 6, 2022
1 parent 62b330d commit cea48c3
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 102 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ ToDo:
- Added .bind() and .focus() methods to almost all widgets
- Added 'anchor' option to CTkOptionMenu and 'justify' option to CTkComboBox
- Added CTkFont class
- Added CTkImage class to replace PIL.ImageTk.PhotoImage, supports scaling and two images for appearance mode, supports configuring


### Changed
- Changed 'text_font' attribute to 'font' in all widgets, changed 'dropdown_text_font' to 'dropdown_font'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ...ctk_toplevel import CTkToplevel
from ..theme.theme_manager import ThemeManager
from ..font.ctk_font import CTkFont
from ..image.ctk_image import CTkImage
from ..appearance_mode.appearance_mode_base_class import CTkAppearanceModeBaseClass
from ..scaling.scaling_base_class import CTkScalingBaseClass

Expand Down Expand Up @@ -145,13 +146,13 @@ def cget(self, attribute_name: str):
else:
raise ValueError(f"'{attribute_name}' is not a supported argument. Look at the documentation for supported arguments.")

@staticmethod
def _check_font_type(font: any):
def _check_font_type(self, font: any):
""" check font type when passed to widget """
if isinstance(font, CTkFont):
return font

elif type(font) == tuple and len(font) == 1:
sys.stderr.write(f"Warning: font {font} given without size, will be extended with default text size of current theme\n")
sys.stderr.write(f"{type(self).__name__} Warning: font {font} given without size, will be extended with default text size of current theme\n")
return font[0], ThemeManager.theme["text"]["size"]

elif type(font) == tuple and 2 <= len(font) <= 3:
Expand All @@ -164,6 +165,17 @@ def _check_font_type(font: any):
f"font=customtkinter.CTkFont(family='<name>', size=<size in px>)\n" +
f"font=('<name>', <size in px>)\n")

def _check_image_type(self, image: any):
""" check image type when passed to widget """
if image is None:
return image
elif isinstance(image, CTkImage):
return image
else:
sys.stderr.write(f"{type(self).__name__} Warning: Given image is not CTkImage but {type(image)}. " +
f"Image can not be scaled on HighDPI displays, use CTkImage instead.\n")
return image

def _update_dimensions_event(self, event):
# only redraw if dimensions changed (for performance), independent of scaling
if round(self._current_width) != round(self._reverse_widget_scaling(event.width)) or round(self._current_height) != round(self._reverse_widget_scaling(event.height)):
Expand Down
4 changes: 2 additions & 2 deletions customtkinter/windows/widgets/ctk_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self,
self._border_spacing = border_spacing

# text, image
self._image = image
self._image = self._check_image_type(image)
self._image_label: Union[tkinter.Label, None] = None
self._text = text
self._text_label: Union[tkinter.Label, None] = None
Expand Down Expand Up @@ -381,7 +381,7 @@ def configure(self, require_redraw=False, **kwargs):
if "image" in kwargs:
if isinstance(self._image, CTkImage):
self._image.remove_configure_callback(self._update_image)
self._image = kwargs.pop("image")
self._image = self._check_image_type(kwargs.pop("image"))
if isinstance(self._image, CTkImage):
self._image.add_configure_callback(self._update_image)
require_redraw = True
Expand Down
8 changes: 8 additions & 0 deletions customtkinter/windows/widgets/ctk_checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ def _draw(self, no_color_updates=False):
self._text_label.configure(bg=self._apply_appearance_mode(self._bg_color))

def configure(self, require_redraw=False, **kwargs):
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")
require_redraw = True

if "checkbox_width" in kwargs:
self._checkbox_width = kwargs.pop("checkbox_width")
self._canvas.configure(width=self._apply_widget_scaling(self._checkbox_width))
Expand Down
63 changes: 31 additions & 32 deletions customtkinter/windows/widgets/ctk_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CTkLabel(CTkBaseClass):

def __init__(self,
master: any = None,
width: int = 140,
width: int = 0,
height: int = 28,
corner_radius: Union[int, str] = "default_theme",

Expand Down Expand Up @@ -53,7 +53,7 @@ def __init__(self,
self._text = text

# image
self._image = image
self._image = self._check_image_type(image)
self._compound = compound
if isinstance(self._image, CTkImage):
self._image.add_configure_callback(self._update_image)
Expand Down Expand Up @@ -85,12 +85,9 @@ def __init__(self,
font=self._apply_font_scaling(self._font))
self._label.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes))

text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))

check_kwargs_empty(kwargs, raise_error=True)

self._create_grid()
self._update_image()
self._draw()

Expand All @@ -100,17 +97,15 @@ def _set_scaling(self, *args, **kwargs):
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width), height=self._apply_widget_scaling(self._desired_height))
self._label.configure(font=self._apply_font_scaling(self._font))

text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))

self._create_grid()
self._draw(no_color_updates=True)

def _set_dimensions(self, width=None, height=None):
super()._set_dimensions(width, height)

self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
height=self._apply_widget_scaling(self._desired_height))
self._create_grid()
self._draw()

def _update_font(self):
Expand All @@ -134,6 +129,13 @@ def destroy(self):
self._font.remove_size_configure_callback(self._update_font)
super().destroy()

def _create_grid(self):
""" configure grid system (1x1) """

text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height / 2))))

def _draw(self, no_color_updates=False):
super()._draw(no_color_updates)

Expand Down Expand Up @@ -164,15 +166,18 @@ def _draw(self, no_color_updates=False):
self._update_image()

def configure(self, require_redraw=False, **kwargs):
if "anchor" in kwargs:
self._anchor = kwargs.pop("anchor")
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
if "corner_radius" in kwargs:
self._corner_radius = kwargs.pop("corner_radius")
self._create_grid()
require_redraw = True

if "compound" in kwargs:
self._compound = kwargs.pop("compound")
self._label.configure(compound=self._compound)
if "fg_color" in kwargs:
self._fg_color = kwargs.pop("fg_color")
require_redraw = True

if "text_color" in kwargs:
self._text_color = kwargs.pop("text_color")
require_redraw = True

if "text" in kwargs:
self._text = kwargs.pop("text")
Expand All @@ -189,25 +194,19 @@ def configure(self, require_redraw=False, **kwargs):
if "image" in kwargs:
if isinstance(self._image, CTkImage):
self._image.remove_configure_callback(self._update_image)
self._image = kwargs.pop("image")
self._image = self._check_image_type(kwargs.pop("image"))
if isinstance(self._image, CTkImage):
self._image.add_configure_callback(self._update_image)
self._update_image()

if "fg_color" in kwargs:
self._fg_color = kwargs.pop("fg_color")
require_redraw = True

if "text_color" in kwargs:
self._text_color = kwargs.pop("text_color")
require_redraw = True
if "compound" in kwargs:
self._compound = kwargs.pop("compound")
self._label.configure(compound=self._compound)

if "corner_radius" in kwargs:
self._corner_radius = kwargs.pop("corner_radius")
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
require_redraw = True
if "anchor" in kwargs:
self._anchor = kwargs.pop("anchor")
self._label.configure(anchor=self._anchor)
self._create_grid()

self._label.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes)) # configure tkinter.Label
super().configure(require_redraw=require_redraw, **kwargs) # configure CTkBaseClass
Expand Down
12 changes: 8 additions & 4 deletions customtkinter/windows/widgets/ctk_progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ def _draw(self, no_color_updates=False):
outline=self._apply_appearance_mode(self._progress_color))

def configure(self, require_redraw=False, **kwargs):
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")
require_redraw = True

if "fg_color" in kwargs:
self._fg_color = kwargs.pop("fg_color")
require_redraw = True
Expand All @@ -165,10 +173,6 @@ def configure(self, require_redraw=False, **kwargs):
self._progress_color = kwargs.pop("progress_color")
require_redraw = True

if "border_width" in kwargs:
self._border_width = kwargs.pop("border_width")
require_redraw = True

if "variable" in kwargs:
if self._variable is not None:
self._variable.trace_remove("write", self._variable_callback_name)
Expand Down
33 changes: 22 additions & 11 deletions customtkinter/windows/widgets/ctk_radiobutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def __init__(self,
self._corner_radius = ThemeManager.theme["shape"]["radiobutton_corner_radius"] if corner_radius == "default_theme" else corner_radius
self._border_width_unchecked = ThemeManager.theme["shape"]["radiobutton_border_width_unchecked"] if border_width_unchecked == "default_theme" else border_width_unchecked
self._border_width_checked = ThemeManager.theme["shape"]["radiobutton_border_width_checked"] if border_width_checked == "default_theme" else border_width_checked
self._border_width = self._border_width_unchecked

# text
self._text = text
Expand Down Expand Up @@ -165,10 +164,16 @@ def destroy(self):
def _draw(self, no_color_updates=False):
super()._draw(no_color_updates)

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))
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))

if no_color_updates is False or requires_recoloring:
self._bg_canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
Expand All @@ -195,6 +200,18 @@ def _draw(self, no_color_updates=False):
self._text_label.configure(bg=self._apply_appearance_mode(self._bg_color))

def configure(self, require_redraw=False, **kwargs):
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

if "radiobutton_width" in kwargs:
self._radiobutton_width = kwargs.pop("radiobutton_width")
self._canvas.configure(width=self._apply_widget_scaling(self._radiobutton_width))
Expand Down Expand Up @@ -239,10 +256,6 @@ def configure(self, require_redraw=False, **kwargs):
self._border_color = kwargs.pop("border_color")
require_redraw = True

if "border_width" in kwargs:
self._border_width = kwargs.pop("border_width")
require_redraw = True

if "hover" in kwargs:
self._hover = kwargs.pop("hover")

Expand Down Expand Up @@ -365,7 +378,6 @@ def invoke(self, event=0):

def select(self, from_variable_callback=False):
self._check_state = True
self._border_width = self._border_width_checked
self._draw()

if self._variable is not None and not from_variable_callback:
Expand All @@ -375,7 +387,6 @@ def select(self, from_variable_callback=False):

def deselect(self, from_variable_callback=False):
self._check_state = False
self._border_width = self._border_width_unchecked
self._draw()

if self._variable is not None and not from_variable_callback:
Expand Down
17 changes: 6 additions & 11 deletions customtkinter/windows/widgets/ctk_tabview.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CTkTabview(CTkBaseClass):
_top_spacing = 10 # px on top of the buttons
_top_button_overhang = 8 # px
_button_height = 26
_segmented_button_border_width = 3

def __init__(self,
master: any = None,
Expand Down Expand Up @@ -67,8 +68,8 @@ def __init__(self,
self._canvas = CTkCanvas(master=self,
bg=self._apply_appearance_mode(self._bg_color),
highlightthickness=0,
width=self._apply_widget_scaling(self._current_width - self._top_spacing - self._top_button_overhang),
height=self._apply_widget_scaling(self._current_height))
width=self._apply_widget_scaling(self._desired_width),
height=self._apply_widget_scaling(self._desired_height - self._top_spacing - self._top_button_overhang))
self._draw_engine = DrawEngine(self._canvas)

self._segmented_button = CTkSegmentedButton(self,
Expand All @@ -82,7 +83,7 @@ def __init__(self,
text_color=text_color,
text_color_disabled=text_color_disabled,
corner_radius=corner_radius,
border_width=self._apply_widget_scaling(3),
border_width=self._apply_widget_scaling(self._segmented_button_border_width),
command=self._segmented_button_callback,
state=state)
self._configure_segmented_button_background_corners()
Expand Down Expand Up @@ -143,14 +144,6 @@ def _configure_segmented_button_background_corners(self):
def _configure_tab_background_corners_by_name(self, name: str):
""" needs to be called for changes in fg_color, bg_color, border_width """

# if self._border_width == 0:
# if self._fg_color is not None:
# self._tab_dict[name].configure(background_corner_colors=(self._fg_color, self._fg_color, self._bg_color, self._bg_color))
# else:
# self._tab_dict[name].configure(background_corner_colors=(self._bg_color, self._bg_color, self._bg_color, self._bg_color))
# else:
# self._tab_dict[name].configure(background_corner_colors=None)

self._tab_dict[name].configure(background_corner_colors=None)

def _configure_grid(self):
Expand Down Expand Up @@ -182,6 +175,8 @@ def _grid_forget_all_tabs(self):

def _create_tab(self) -> CTkFrame:
new_tab = CTkFrame(self,
height=0,
width=0,
fg_color=self._fg_color,
border_width=0,
corner_radius=self._corner_radius)
Expand Down
4 changes: 2 additions & 2 deletions customtkinter/windows/widgets/ctk_textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def __init__(self,

self._canvas = CTkCanvas(master=self,
highlightthickness=0,
width=self._apply_widget_scaling(self._current_width),
height=self._apply_widget_scaling(self._current_height))
width=self._apply_widget_scaling(self._desired_width),
height=self._apply_widget_scaling(self._desired_height))
self._canvas.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="nsew")
self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
self._draw_engine = DrawEngine(self._canvas)
Expand Down
Loading

0 comments on commit cea48c3

Please sign in to comment.