fixed scaling issues

This commit is contained in:
Tom Schimansky 2022-04-21 20:30:46 +02:00
parent 6342bf8034
commit 245a8d81bc
6 changed files with 43 additions and 7 deletions

View File

@ -1,4 +1,5 @@
import sys
import math
import tkinter
from typing import Union
@ -55,9 +56,19 @@ class CTkDrawEngine:
returns bool if recoloring is necessary """
print("before", width, height)
width = math.floor(width / 2) * 2 # round width and height and restrict them to even values only
height = math.floor(height / 2) * 2
corner_radius = round(corner_radius)
print("after", width, height)
if corner_radius > width / 2 or corner_radius > height / 2: # restrict corner_radius if it's too larger
corner_radius = min(width / 2, height / 2)
print("corner", corner_radius)
border_width = round(border_width)
corner_radius = self._calc_optimal_corner_radius(corner_radius) # optimize corner_radius for different drawing methods (different rounding)
@ -346,6 +357,9 @@ class CTkDrawEngine:
returns bool if recoloring is necessary """
width = math.floor(width / 2) * 2 # round width and height and restrict them to even values only
height = math.floor(height / 2) * 2
if corner_radius > width / 2 or corner_radius > height / 2: # restrict corner_radius if it's too larger
corner_radius = min(width / 2, height / 2)
@ -507,6 +521,9 @@ class CTkDrawEngine:
button_length: Union[float, int], button_corner_radius: Union[float, int], slider_value: float,
orientation: str) -> bool:
width = math.floor(width / 2) * 2 # round width and height and restrict them to even values only
height = math.floor(height / 2) * 2
if corner_radius > width / 2 or corner_radius > height / 2: # restrict corner_radius if it's too larger
corner_radius = min(width / 2, height / 2)

View File

@ -72,6 +72,12 @@ class CTkCheckBox(CTkBaseClass):
self.grid_columnconfigure(2, weight=1)
self.grid_rowconfigure(0, weight=1)
self.bg_canvas = CTkCanvas(master=self,
highlightthickness=0,
width=self.width * self.scaling,
height=self.height * self.scaling)
self.bg_canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=3, rowspan=1, sticky="nswe")
self.canvas = CTkCanvas(master=self,
highlightthickness=0,
width=self.width * self.scaling,
@ -116,7 +122,7 @@ class CTkCheckBox(CTkBaseClass):
else:
self.canvas.delete("checkmark")
self.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.bg_canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
if self.check_state is True:

View File

@ -68,6 +68,12 @@ class CTkRadioButton(CTkBaseClass):
self.grid_columnconfigure(1, weight=0, minsize=6 * self.scaling)
self.grid_columnconfigure(2, weight=1)
self.bg_canvas = CTkCanvas(master=self,
highlightthickness=0,
width=self.width * self.scaling,
height=self.height * self.scaling)
self.bg_canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=3, rowspan=1, sticky="nswe")
self.canvas = CTkCanvas(master=self,
highlightthickness=0,
width=self.width * self.scaling,
@ -102,8 +108,8 @@ class CTkRadioButton(CTkBaseClass):
self.corner_radius * self.scaling,
self.border_width * self.scaling)
self.bg_canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
if self.check_state is False:
self.canvas.itemconfig("border_parts",

View File

@ -73,6 +73,12 @@ class CTkSwitch(CTkBaseClass):
self.grid_columnconfigure(1, weight=0, minsize=6 * self.scaling)
self.grid_columnconfigure(2, weight=0)
self.bg_canvas = CTkCanvas(master=self,
highlightthickness=0,
width=self.width * self.scaling,
height=self.height * self.scaling)
self.bg_canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=3, rowspan=1, sticky="nswe")
self.canvas = CTkCanvas(master=self,
highlightthickness=0,
width=self.width * self.scaling,
@ -127,7 +133,7 @@ class CTkSwitch(CTkBaseClass):
0, "w")
if no_color_updates is False or requires_recoloring:
self.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.bg_canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
if self.border_color is None:

View File

@ -2,6 +2,7 @@ import tkinter
import tkinter.ttk as ttk
import copy
import re
import math
from .ctk_tk import CTk
from .ctk_toplevel import CTkToplevel
@ -76,9 +77,9 @@ class CTkBaseClass(tkinter.Frame):
def update_dimensions_event(self, event):
# only redraw if dimensions changed (for performance)
if self.width != int(event.width * self.scaling) or self.height != int(event.height * self.scaling):
self.width = int(event.width / self.scaling) # adjust current size according to new size given by event
self.height = int(event.height / self.scaling) # width and height are independent of the scale
if self.width != math.floor(event.width * self.scaling) or self.height != math.floor(event.height * self.scaling):
self.width = event.width / self.scaling # adjust current size according to new size given by event
self.height = event.height / self.scaling # width and height are independent of the scale
self.draw(no_color_updates=True) # faster drawing without color changes

View File

@ -4,7 +4,7 @@ import customtkinter # <- import the CustomTkinter module
customtkinter.set_appearance_mode("dark") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
customtkinter.ScalingTracker.set_user_scaling(0.9)
customtkinter.ScalingTracker.set_user_scaling(2.5)
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
root_tk.geometry("400x480")