fixed complex example

This commit is contained in:
Tom Schimansky 2022-05-04 15:38:13 +02:00
parent e0eebac62c
commit 9ee5e3b7f2
3 changed files with 22 additions and 9 deletions

View File

@ -66,8 +66,8 @@ class CTkButton(CTkBaseClass):
self.canvas = CTkCanvas(master=self, self.canvas = CTkCanvas(master=self,
highlightthickness=0, highlightthickness=0,
width=self.apply_widget_scaling(self.desired_width), width=self.round_size(self.apply_widget_scaling(self.desired_width)),
height=self.apply_widget_scaling(self.desired_height)) height=self.round_size(self.apply_widget_scaling(self.desired_height)))
self.canvas.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="nsew") self.canvas.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="nsew")
self.draw_engine = CTkDrawEngine(self.canvas, CTkSettings.preferred_drawing_method) self.draw_engine = CTkDrawEngine(self.canvas, CTkSettings.preferred_drawing_method)
@ -98,10 +98,12 @@ class CTkButton(CTkBaseClass):
self.image_label.destroy() self.image_label.destroy()
self.image_label = None self.image_label = None
self.canvas.configure(width=self.apply_widget_scaling(self.desired_width), height=self.apply_widget_scaling(self.desired_height)) self.canvas.configure(width=self.round_size(self.apply_widget_scaling(self.desired_width)),
height=self.round_size(self.apply_widget_scaling(self.desired_height)))
self.draw() self.draw()
def draw(self, no_color_updates=False): def draw(self, no_color_updates=False):
print("current_height", self.current_height, "desired", self.desired_height)
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self.current_width), 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.current_height),
self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(self.corner_radius),
@ -219,6 +221,8 @@ class CTkButton(CTkBaseClass):
self.text_label.grid(row=0, column=0, sticky="s", columnspan=2, rowspan=1, self.text_label.grid(row=0, column=0, sticky="s", columnspan=2, rowspan=1,
padx=max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(self.border_width)), pady=(self.apply_widget_scaling(self.border_width), 2)) padx=max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(self.border_width)), pady=(self.apply_widget_scaling(self.border_width), 2))
# self.canvas.configure(bg="red") # test
def configure(self, *args, **kwargs): def configure(self, *args, **kwargs):
require_redraw = False # some attribute changes require a call of self.draw() at the end require_redraw = False # some attribute changes require a call of self.draw() at the end

View File

@ -2,6 +2,7 @@ import tkinter
import tkinter.ttk as ttk import tkinter.ttk as ttk
import copy import copy
import re import re
import math
from typing import Callable, Union, TypedDict from typing import Callable, Union, TypedDict
from ..windows.ctk_tk import CTk from ..windows.ctk_tk import CTk
@ -27,7 +28,8 @@ class CTkBaseClass(tkinter.Frame):
self.widget_scaling = ScalingTracker.get_widget_scaling(self) self.widget_scaling = ScalingTracker.get_widget_scaling(self)
self.spacing_scaling = ScalingTracker.get_spacing_scaling(self) self.spacing_scaling = ScalingTracker.get_spacing_scaling(self)
super().configure(width=self.apply_widget_scaling(self.desired_width), height=self.apply_widget_scaling(self.desired_height)) super().configure(width=self.round_size(self.apply_widget_scaling(self.desired_width)),
height=self.round_size(self.apply_widget_scaling(self.desired_height)))
# save latest geometry function and kwargs # save latest geometry function and kwargs
class GeometryCallDict(TypedDict): class GeometryCallDict(TypedDict):
@ -123,9 +125,9 @@ class CTkBaseClass(tkinter.Frame):
def update_dimensions_event(self, event): def update_dimensions_event(self, event):
# only redraw if dimensions changed (for performance) # only redraw if dimensions changed (for performance)
if self.current_width != round(event.width / self.widget_scaling) or self.current_height != round(event.height / self.widget_scaling): if round(self.current_width) != round(event.width / self.widget_scaling) or round(self.current_height) != round(event.height / self.widget_scaling):
self.current_width = round(event.width / self.widget_scaling) # adjust current size according to new size given by event self.current_width = (event.width / self.widget_scaling) # adjust current size according to new size given by event
self.current_height = round(event.height / self.widget_scaling) # current_width and current_height are independent of the scale self.current_height = (event.height / self.widget_scaling) # current_width and current_height are independent of the scale
self.draw(no_color_updates=True) # faster drawing without color changes self.draw(no_color_updates=True) # faster drawing without color changes
@ -165,7 +167,8 @@ class CTkBaseClass(tkinter.Frame):
self.widget_scaling = new_widget_scaling self.widget_scaling = new_widget_scaling
self.spacing_scaling = new_spacing_scaling self.spacing_scaling = new_spacing_scaling
super().configure(width=self.apply_widget_scaling(self.desired_width), height=self.apply_widget_scaling(self.desired_height)) super().configure(width=self.round_size(self.apply_widget_scaling(self.desired_width)),
height=self.round_size(self.apply_widget_scaling(self.desired_height)))
if self.last_geometry_manager_call is not None: if self.last_geometry_manager_call is not None:
self.last_geometry_manager_call["function"](**self.apply_argument_scaling(self.last_geometry_manager_call["kwargs"])) self.last_geometry_manager_call["function"](**self.apply_argument_scaling(self.last_geometry_manager_call["kwargs"]))
@ -204,6 +207,13 @@ class CTkBaseClass(tkinter.Frame):
else: else:
return font return font
@staticmethod
def round_size(size: Union[int, float, str]):
if isinstance(size, (int, float)):
return math.floor(size / 2) * 2
else:
return size
def draw(self, no_color_updates=False): def draw(self, no_color_updates=False):
""" abstract of draw method to be overridden """ """ abstract of draw method to be overridden """
pass pass

View File

@ -3,7 +3,6 @@ import tkinter.messagebox
import customtkinter import customtkinter
import sys import sys
customtkinter.ScalingTracker.set_user_scaling(0.5)
customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light" customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue" customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"