added return value of geometry method for CTk and CTkToplevel

This commit is contained in:
Tom Schimansky 2022-07-17 21:41:13 +02:00
parent b3c0388958
commit 67f2072e07
2 changed files with 65 additions and 24 deletions

View File

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

View File

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