From 67f2072e0759dac95f9908ee8ec0954f1d5ec85b Mon Sep 17 00:00:00 2001 From: Tom Schimansky Date: Sun, 17 Jul 2022 21:41:13 +0200 Subject: [PATCH] added return value of geometry method for CTk and CTkToplevel --- customtkinter/windows/ctk_tk.py | 40 ++++++++++++++++++---- customtkinter/windows/ctk_toplevel.py | 49 +++++++++++++++++---------- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/customtkinter/windows/ctk_tk.py b/customtkinter/windows/ctk_tk.py index b561751..7cbfedf 100644 --- a/customtkinter/windows/ctk_tk.py +++ b/customtkinter/windows/ctk_tk.py @@ -129,16 +129,42 @@ class CTk(tkinter.Tk): if self.current_height > height: self.current_height = height super().maxsize(self.apply_window_scaling(self.max_width), self.apply_window_scaling(self.max_height)) - def geometry(self, geometry_string): - super().geometry(self.apply_geometry_scaling(geometry_string)) + def geometry(self, geometry_string: str = None): + if geometry_string is not None: + super().geometry(self.apply_geometry_scaling(geometry_string)) - # update width and height attributes - numbers = list(map(int, re.split(r"[x+]", geometry_string))) # split geometry string into list of numbers - self.current_width = max(self.min_width, min(numbers[0], self.max_width)) # bound value between min and max - self.current_height = max(self.min_height, min(numbers[1], self.max_height)) + # update width and height attributes + numbers = list(map(int, re.split(r"[x+-]", geometry_string))) # split geometry string into list of numbers + self.current_width = max(self.min_width, min(numbers[0], self.max_width)) # bound value between min and max + self.current_height = max(self.min_height, min(numbers[1], self.max_height)) + else: + return self.reverse_geometry_scaling(super().geometry()) def apply_geometry_scaling(self, geometry_string): - return re.sub(re.compile("\d+"), lambda match_obj: str(round(int(match_obj.group(0)) * self.window_scaling)), geometry_string) + value_list = re.split(r"[x+-]", geometry_string) + separator_list = re.split(r"\d+", geometry_string) + + if len(value_list) == 2: + scaled_width = str(round(int(value_list[0]) * self.window_scaling)) + scaled_height = str(round(int(value_list[1]) * self.window_scaling)) + return f"{scaled_width}x{scaled_height}" + elif len(value_list) == 4: + scaled_width = str(round(int(value_list[0]) * self.window_scaling)) + scaled_height = str(round(int(value_list[1]) * self.window_scaling)) + return f"{scaled_width}x{scaled_height}{separator_list[1]}{value_list[2]}{separator_list[2]}{value_list[3]}" + + def reverse_geometry_scaling(self, scaled_geometry_string): + value_list = re.split(r"[x+-]", scaled_geometry_string) + separator_list = re.split(r"\d+", scaled_geometry_string) + + if len(value_list) == 2: + width = str(round(int(value_list[0]) / self.window_scaling)) + height = str(round(int(value_list[1]) / self.window_scaling)) + return f"{width}x{height}" + elif len(value_list) == 4: + width = str(round(int(value_list[0]) / self.window_scaling)) + height = str(round(int(value_list[1]) / self.window_scaling)) + return f"{width}x{height}{separator_list[1]}{value_list[2]}{separator_list[2]}{value_list[3]}" def apply_window_scaling(self, value): if isinstance(value, (int, float)): diff --git a/customtkinter/windows/ctk_toplevel.py b/customtkinter/windows/ctk_toplevel.py index b8c853c..10adbbc 100644 --- a/customtkinter/windows/ctk_toplevel.py +++ b/customtkinter/windows/ctk_toplevel.py @@ -84,18 +84,30 @@ class CTkToplevel(tkinter.Toplevel): super().maxsize(self.apply_window_scaling(self.max_width), self.apply_window_scaling(self.max_height)) def apply_geometry_scaling(self, geometry_string): - numbers = list(map(int, re.split(r"[x+]", geometry_string))) # split geometry string into list of numbers + value_list = re.split(r"[x+-]", geometry_string) + separator_list = re.split(r"\d+", geometry_string) - if len(numbers) == 2: - return f"{self.apply_window_scaling(numbers[0]):.0f}x" +\ - f"{self.apply_window_scaling(numbers[1]):.0f}" - elif len(numbers) == 4: - return f"{self.apply_window_scaling(numbers[0]):.0f}x" +\ - f"{self.apply_window_scaling(numbers[1]):.0f}+" +\ - f"{self.apply_window_scaling(numbers[2]):.0f}+" +\ - f"{self.apply_window_scaling(numbers[3]):.0f}" - else: - return geometry_string + if len(value_list) == 2: + scaled_width = str(round(int(value_list[0]) * self.window_scaling)) + scaled_height = str(round(int(value_list[1]) * self.window_scaling)) + return f"{scaled_width}x{scaled_height}" + elif len(value_list) == 4: + scaled_width = str(round(int(value_list[0]) * self.window_scaling)) + scaled_height = str(round(int(value_list[1]) * self.window_scaling)) + return f"{scaled_width}x{scaled_height}{separator_list[1]}{value_list[2]}{separator_list[2]}{value_list[3]}" + + def reverse_geometry_scaling(self, scaled_geometry_string): + value_list = re.split(r"[x+-]", scaled_geometry_string) + separator_list = re.split(r"\d+", scaled_geometry_string) + + if len(value_list) == 2: + width = str(round(int(value_list[0]) / self.window_scaling)) + height = str(round(int(value_list[1]) / self.window_scaling)) + return f"{width}x{height}" + elif len(value_list) == 4: + width = str(round(int(value_list[0]) / self.window_scaling)) + height = str(round(int(value_list[1]) / self.window_scaling)) + return f"{width}x{height}{separator_list[1]}{value_list[2]}{separator_list[2]}{value_list[3]}" def apply_window_scaling(self, value): if isinstance(value, (int, float)): @@ -103,13 +115,16 @@ class CTkToplevel(tkinter.Toplevel): else: return value - def geometry(self, geometry_string): - super().geometry(self.apply_geometry_scaling(geometry_string)) + def geometry(self, geometry_string: str = None): + if geometry_string is not None: + super().geometry(self.apply_geometry_scaling(geometry_string)) - # update width and height attributes - numbers = list(map(int, re.split(r"[x+]", geometry_string))) # split geometry string into list of numbers - self.current_width = max(self.min_width, min(numbers[0], self.max_width)) # bound value between min and max - self.current_height = max(self.min_height, min(numbers[1], self.max_height)) + # update width and height attributes + numbers = list(map(int, re.split(r"[x+]", geometry_string))) # split geometry string into list of numbers + self.current_width = max(self.min_width, min(numbers[0], self.max_width)) # bound value between min and max + self.current_height = max(self.min_height, min(numbers[1], self.max_height)) + else: + return self.reverse_geometry_scaling(super().geometry()) def destroy(self): AppearanceModeTracker.remove(self.set_appearance_mode)