mirror of
https://github.com/TomSchimansky/CustomTkinter.git
synced 2023-08-10 21:13:13 +03:00
added type hints and private hints to all widget classes, fixed #497, removed get and set methods from some widgets
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import tkinter
|
||||
import time
|
||||
from typing import Union, Tuple
|
||||
|
||||
from ..widgets.ctk_label import CTkLabel
|
||||
from ..widgets.ctk_entry import CTkEntry
|
||||
@ -11,109 +12,111 @@ from ..theme_manager import ThemeManager
|
||||
|
||||
|
||||
class CTkInputDialog:
|
||||
def __init__(self,
|
||||
master=None,
|
||||
title="CTkDialog",
|
||||
text="CTkDialog",
|
||||
fg_color="default_theme",
|
||||
hover_color="default_theme",
|
||||
border_color="default_theme"):
|
||||
"""
|
||||
Dialog with extra window, message, entry widget, cancel and ok button.
|
||||
For detailed information check out the documentation.
|
||||
"""
|
||||
|
||||
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
def __init__(self,
|
||||
master: any = None,
|
||||
title: str = "CTkDialog",
|
||||
text: str = "CTkDialog",
|
||||
fg_color: Union[str, Tuple[str, str]] = "default_theme",
|
||||
hover_color: Union[str, Tuple[str, str]] = "default_theme",
|
||||
border_color: Union[str, Tuple[str, str]] = "default_theme"):
|
||||
|
||||
self._appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
self.master = master
|
||||
|
||||
self.user_input = None
|
||||
self.running = False
|
||||
self._window_bg_color = ThemeManager.theme["color"]["window_bg_color"]
|
||||
self._fg_color = ThemeManager.theme["color"]["button"] if fg_color == "default_theme" else fg_color
|
||||
self._hover_color = ThemeManager.theme["color"]["button_hover"] if hover_color == "default_theme" else hover_color
|
||||
self._border_color = ThemeManager.theme["color"]["button_hover"] if border_color == "default_theme" else border_color
|
||||
|
||||
self.height = len(text.split("\n"))*20 + 150
|
||||
self._user_input: Union[str, None] = None
|
||||
self._running: bool = False
|
||||
self._height: int = len(text.split("\n")) * 20 + 150
|
||||
self._text = text
|
||||
|
||||
self.text = text
|
||||
self.window_bg_color = ThemeManager.theme["color"]["window_bg_color"]
|
||||
self.fg_color = ThemeManager.theme["color"]["button"] if fg_color == "default_theme" else fg_color
|
||||
self.hover_color = ThemeManager.theme["color"]["button_hover"] if hover_color == "default_theme" else hover_color
|
||||
self.border_color = ThemeManager.theme["color"]["button_hover"] if border_color == "default_theme" else border_color
|
||||
self._toplevel_window = CTkToplevel()
|
||||
self._toplevel_window.geometry(f"{280}x{self._height}")
|
||||
self._toplevel_window.minsize(280, self._height)
|
||||
self._toplevel_window.maxsize(280, self._height)
|
||||
self._toplevel_window.title(title)
|
||||
self._toplevel_window.lift()
|
||||
self._toplevel_window.focus_force()
|
||||
self._toplevel_window.grab_set()
|
||||
self._toplevel_window.protocol("WM_DELETE_WINDOW", self._on_closing)
|
||||
self._toplevel_window.after(10, self._create_widgets) # create widgets with slight delay, to avoid white flickering of background
|
||||
|
||||
self.top = CTkToplevel()
|
||||
self.top.geometry(f"{280}x{self.height}")
|
||||
self.top.minsize(280, self.height)
|
||||
self.top.maxsize(280, self.height)
|
||||
self.top.title(title)
|
||||
self.top.lift()
|
||||
self.top.focus_force()
|
||||
self.top.grab_set()
|
||||
def _create_widgets(self):
|
||||
self._label_frame = CTkFrame(master=self._toplevel_window,
|
||||
corner_radius=0,
|
||||
fg_color=self._window_bg_color,
|
||||
width=300,
|
||||
height=self._height-100)
|
||||
self._label_frame.place(relx=0.5, rely=0, anchor=tkinter.N)
|
||||
|
||||
self.top.protocol("WM_DELETE_WINDOW", self.on_closing)
|
||||
self._button_and_entry_frame = CTkFrame(master=self._toplevel_window,
|
||||
corner_radius=0,
|
||||
fg_color=self._window_bg_color,
|
||||
width=300,
|
||||
height=100)
|
||||
self._button_and_entry_frame.place(relx=0.5, rely=1, anchor=tkinter.S)
|
||||
|
||||
self.top.after(10, self.create_widgets) # create widgets with slight delay, to avoid white flickering of background
|
||||
self._myLabel = CTkLabel(master=self._label_frame,
|
||||
text=self._text,
|
||||
width=300,
|
||||
fg_color=None,
|
||||
height=self._height-100)
|
||||
self._myLabel.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
|
||||
|
||||
def create_widgets(self):
|
||||
self.label_frame = CTkFrame(master=self.top,
|
||||
corner_radius=0,
|
||||
fg_color=self.window_bg_color,
|
||||
width=300,
|
||||
height=self.height-100)
|
||||
self.label_frame.place(relx=0.5, rely=0, anchor=tkinter.N)
|
||||
self._entry = CTkEntry(master=self._button_and_entry_frame,
|
||||
width=230)
|
||||
self._entry.place(relx=0.5, rely=0.15, anchor=tkinter.CENTER)
|
||||
|
||||
self.button_and_entry_frame = CTkFrame(master=self.top,
|
||||
corner_radius=0,
|
||||
fg_color=self.window_bg_color,
|
||||
width=300,
|
||||
height=100)
|
||||
self.button_and_entry_frame.place(relx=0.5, rely=1, anchor=tkinter.S)
|
||||
self._ok_button = CTkButton(master=self._button_and_entry_frame,
|
||||
text='Ok',
|
||||
width=100,
|
||||
command=self._ok_event,
|
||||
fg_color=self._fg_color,
|
||||
hover_color=self._hover_color,
|
||||
border_color=self._border_color)
|
||||
self._ok_button.place(relx=0.28, rely=0.65, anchor=tkinter.CENTER)
|
||||
|
||||
self.myLabel = CTkLabel(master=self.label_frame,
|
||||
text=self.text,
|
||||
width=300,
|
||||
fg_color=None,
|
||||
height=self.height-100)
|
||||
self.myLabel.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
|
||||
self._cancel_button = CTkButton(master=self._button_and_entry_frame,
|
||||
text='Cancel',
|
||||
width=100,
|
||||
command=self._cancel_event,
|
||||
fg_color=self._fg_color,
|
||||
hover_color=self._hover_color,
|
||||
border_color=self._border_color)
|
||||
self._cancel_button.place(relx=0.72, rely=0.65, anchor=tkinter.CENTER)
|
||||
|
||||
self.entry = CTkEntry(master=self.button_and_entry_frame,
|
||||
width=230)
|
||||
self.entry.place(relx=0.5, rely=0.15, anchor=tkinter.CENTER)
|
||||
self._entry.focus_force()
|
||||
self._entry.bind("<Return>", self._ok_event)
|
||||
|
||||
self.ok_button = CTkButton(master=self.button_and_entry_frame,
|
||||
text='Ok',
|
||||
width=100,
|
||||
command=self.ok_event,
|
||||
fg_color=self.fg_color,
|
||||
hover_color=self.hover_color,
|
||||
border_color=self.border_color)
|
||||
self.ok_button.place(relx=0.28, rely=0.65, anchor=tkinter.CENTER)
|
||||
def _ok_event(self, event=None):
|
||||
self._user_input = self._entry.get()
|
||||
self._running = False
|
||||
|
||||
self.cancel_button = CTkButton(master=self.button_and_entry_frame,
|
||||
text='Cancel',
|
||||
width=100,
|
||||
command=self.cancel_event,
|
||||
fg_color=self.fg_color,
|
||||
hover_color=self.hover_color,
|
||||
border_color=self.border_color)
|
||||
self.cancel_button.place(relx=0.72, rely=0.65, anchor=tkinter.CENTER)
|
||||
def _on_closing(self):
|
||||
self._running = False
|
||||
|
||||
self.entry.entry.focus_force()
|
||||
self.entry.bind("<Return>", self.ok_event)
|
||||
|
||||
def ok_event(self, event=None):
|
||||
self.user_input = self.entry.get()
|
||||
self.running = False
|
||||
|
||||
def on_closing(self):
|
||||
self.running = False
|
||||
|
||||
def cancel_event(self):
|
||||
self.running = False
|
||||
def _cancel_event(self):
|
||||
self._running = False
|
||||
|
||||
def get_input(self):
|
||||
self.running = True
|
||||
self._running = True
|
||||
|
||||
while self.running:
|
||||
while self._running:
|
||||
try:
|
||||
self.top.update()
|
||||
self._toplevel_window.update()
|
||||
except Exception:
|
||||
return self.user_input
|
||||
return self._user_input
|
||||
finally:
|
||||
time.sleep(0.01)
|
||||
|
||||
time.sleep(0.05)
|
||||
self.top.destroy()
|
||||
return self.user_input
|
||||
self._toplevel_window.destroy()
|
||||
return self._user_input
|
||||
|
@ -14,126 +14,129 @@ from ..settings import Settings
|
||||
|
||||
|
||||
class CTk(tkinter.Tk):
|
||||
"""
|
||||
Main app window with dark titlebar on Windows and macOS.
|
||||
For detailed information check out the documentation.
|
||||
"""
|
||||
|
||||
def __init__(self, *args,
|
||||
fg_color="default_theme",
|
||||
fg_color: Union[str, Tuple[str, str]] = "default_theme",
|
||||
**kwargs):
|
||||
|
||||
ScalingTracker.activate_high_dpi_awareness() # make process DPI aware
|
||||
self.enable_macos_dark_title_bar()
|
||||
self._enable_macos_dark_title_bar()
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
# add set_appearance_mode method to callback list of AppearanceModeTracker for appearance mode changes
|
||||
AppearanceModeTracker.add(self.set_appearance_mode, self)
|
||||
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
AppearanceModeTracker.add(self._set_appearance_mode, self)
|
||||
self._appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
|
||||
# add set_scaling method to callback list of ScalingTracker for automatic scaling changes
|
||||
ScalingTracker.add_widget(self.set_scaling, self)
|
||||
self.window_scaling = ScalingTracker.get_window_scaling(self)
|
||||
ScalingTracker.add_widget(self._set_scaling, self)
|
||||
self._window_scaling = ScalingTracker.get_window_scaling(self)
|
||||
|
||||
self.current_width = 600 # initial window size, always without scaling
|
||||
self.current_height = 500
|
||||
self.min_width: int = 0
|
||||
self.min_height: int = 0
|
||||
self.max_width: int = 1_000_000
|
||||
self.max_height: int = 1_000_000
|
||||
self.last_resizable_args: Union[Tuple[list, dict], None] = None # (args, kwargs)
|
||||
self._current_width = 600 # initial window size, always without scaling
|
||||
self._current_height = 500
|
||||
self._min_width: int = 0
|
||||
self._min_height: int = 0
|
||||
self._max_width: int = 1_000_000
|
||||
self._max_height: int = 1_000_000
|
||||
self._last_resizable_args: Union[Tuple[list, dict], None] = None # (args, kwargs)
|
||||
|
||||
self.fg_color = ThemeManager.theme["color"]["window_bg_color"] if fg_color == "default_theme" else fg_color
|
||||
self._fg_color = ThemeManager.theme["color"]["window_bg_color"] if fg_color == "default_theme" else fg_color
|
||||
|
||||
if "bg" in kwargs:
|
||||
self.fg_color = kwargs["bg"]
|
||||
del kwargs["bg"]
|
||||
self._fg_color = kwargs.pop("bg")
|
||||
elif "background" in kwargs:
|
||||
self.fg_color = kwargs["background"]
|
||||
del kwargs["background"]
|
||||
self._fg_color = kwargs.pop("background")
|
||||
|
||||
super().configure(bg=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
super().configure(bg=ThemeManager.single_color(self._fg_color, self._appearance_mode))
|
||||
super().title("CTk")
|
||||
self.geometry(f"{self.current_width}x{self.current_height}")
|
||||
self.geometry(f"{self._current_width}x{self._current_height}")
|
||||
|
||||
self.state_before_windows_set_titlebar_color = None
|
||||
self.window_exists = False # indicates if the window is already shown through update() or mainloop() after init
|
||||
self.withdraw_called_before_window_exists = False # indicates if withdraw() was called before window is first shown through update() or mainloop()
|
||||
self.iconify_called_before_window_exists = False # indicates if iconify() was called before window is first shown through update() or mainloop()
|
||||
self._state_before_windows_set_titlebar_color = None
|
||||
self._window_exists = False # indicates if the window is already shown through update() or mainloop() after init
|
||||
self._withdraw_called_before_window_exists = False # indicates if withdraw() was called before window is first shown through update() or mainloop()
|
||||
self._iconify_called_before_window_exists = False # indicates if iconify() was called before window is first shown through update() or mainloop()
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if self.appearance_mode == 1:
|
||||
self.windows_set_titlebar_color("dark")
|
||||
if self._appearance_mode == 1:
|
||||
self._windows_set_titlebar_color("dark")
|
||||
else:
|
||||
self.windows_set_titlebar_color("light")
|
||||
self._windows_set_titlebar_color("light")
|
||||
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
self.bind('<Configure>', self._update_dimensions_event)
|
||||
|
||||
self.block_update_dimensions_event = False
|
||||
self._block_update_dimensions_event = False
|
||||
|
||||
def update_dimensions_event(self, event=None):
|
||||
if not self.block_update_dimensions_event:
|
||||
def _update_dimensions_event(self, event=None):
|
||||
if not self._block_update_dimensions_event:
|
||||
detected_width = self.winfo_width() # detect current window size
|
||||
detected_height = self.winfo_height()
|
||||
|
||||
if self.current_width != round(detected_width / self.window_scaling) or self.current_height != round(detected_height / self.window_scaling):
|
||||
self.current_width = round(detected_width / self.window_scaling) # adjust current size according to new size given by event
|
||||
self.current_height = round(detected_height / self.window_scaling) # _current_width and _current_height are independent of the scale
|
||||
if self._current_width != round(detected_width / self._window_scaling) or self._current_height != round(detected_height / self._window_scaling):
|
||||
self._current_width = round(detected_width / self._window_scaling) # adjust current size according to new size given by event
|
||||
self._current_height = round(detected_height / self._window_scaling) # _current_width and _current_height are independent of the scale
|
||||
|
||||
def set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
self.window_scaling = new_window_scaling
|
||||
def _set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
self._window_scaling = new_window_scaling
|
||||
|
||||
# block update_dimensions_event to prevent current_width and current_height to get updated
|
||||
self.block_update_dimensions_event = True
|
||||
self._block_update_dimensions_event = True
|
||||
|
||||
# force new dimensions on window by using min, max, and geometry
|
||||
super().minsize(self.apply_window_scaling(self.current_width), self.apply_window_scaling(self.current_height))
|
||||
super().maxsize(self.apply_window_scaling(self.current_width), self.apply_window_scaling(self.current_height))
|
||||
super().geometry(f"{self.apply_window_scaling(self.current_width)}x"+f"{self.apply_window_scaling(self.current_height)}")
|
||||
super().minsize(self._apply_window_scaling(self._current_width), self._apply_window_scaling(self._current_height))
|
||||
super().maxsize(self._apply_window_scaling(self._current_width), self._apply_window_scaling(self._current_height))
|
||||
super().geometry(f"{self._apply_window_scaling(self._current_width)}x" + f"{self._apply_window_scaling(self._current_height)}")
|
||||
|
||||
# set new scaled min and max with 400ms delay (otherwise it won't work for some reason)
|
||||
self.after(400, self.set_scaled_min_max)
|
||||
self.after(400, self._set_scaled_min_max)
|
||||
|
||||
# release the blocking of update_dimensions_event after a small amount of time (slight delay is necessary)
|
||||
def set_block_update_dimensions_event_false():
|
||||
self.block_update_dimensions_event = False
|
||||
self._block_update_dimensions_event = False
|
||||
self.after(100, lambda: set_block_update_dimensions_event_false())
|
||||
|
||||
def set_scaled_min_max(self):
|
||||
if self.min_width is not None or self.min_height is not None:
|
||||
super().minsize(self.apply_window_scaling(self.min_width), self.apply_window_scaling(self.min_height))
|
||||
if self.max_width is not None or self.max_height is not None:
|
||||
super().maxsize(self.apply_window_scaling(self.max_width), self.apply_window_scaling(self.max_height))
|
||||
def _set_scaled_min_max(self):
|
||||
if self._min_width is not None or self._min_height is not None:
|
||||
super().minsize(self._apply_window_scaling(self._min_width), self._apply_window_scaling(self._min_height))
|
||||
if self._max_width is not None or self._max_height is not None:
|
||||
super().maxsize(self._apply_window_scaling(self._max_width), self._apply_window_scaling(self._max_height))
|
||||
|
||||
def destroy(self):
|
||||
AppearanceModeTracker.remove(self.set_appearance_mode)
|
||||
ScalingTracker.remove_window(self.set_scaling, self)
|
||||
self.disable_macos_dark_title_bar()
|
||||
AppearanceModeTracker.remove(self._set_appearance_mode)
|
||||
ScalingTracker.remove_window(self._set_scaling, self)
|
||||
self._disable_macos_dark_title_bar()
|
||||
super().destroy()
|
||||
|
||||
def withdraw(self):
|
||||
if self.window_exists is False:
|
||||
self.withdraw_called_before_window_exists = True
|
||||
if self._window_exists is False:
|
||||
self._withdraw_called_before_window_exists = True
|
||||
super().withdraw()
|
||||
|
||||
def iconify(self):
|
||||
if self.window_exists is False:
|
||||
self.iconify_called_before_window_exists = True
|
||||
if self._window_exists is False:
|
||||
self._iconify_called_before_window_exists = True
|
||||
super().iconify()
|
||||
|
||||
def update(self):
|
||||
if self.window_exists is False:
|
||||
self.window_exists = True
|
||||
if self._window_exists is False:
|
||||
self._window_exists = True
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if not self.withdraw_called_before_window_exists and not self.iconify_called_before_window_exists:
|
||||
if not self._withdraw_called_before_window_exists and not self._iconify_called_before_window_exists:
|
||||
# print("window dont exists -> deiconify in update")
|
||||
self.deiconify()
|
||||
|
||||
super().update()
|
||||
|
||||
def mainloop(self, *args, **kwargs):
|
||||
if not self.window_exists:
|
||||
self.window_exists = True
|
||||
if not self._window_exists:
|
||||
self._window_exists = True
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if not self.withdraw_called_before_window_exists and not self.iconify_called_before_window_exists:
|
||||
if not self._withdraw_called_before_window_exists and not self._iconify_called_before_window_exists:
|
||||
# print("window dont exists -> deiconify in mainloop")
|
||||
self.deiconify()
|
||||
|
||||
@ -141,42 +144,46 @@ class CTk(tkinter.Tk):
|
||||
|
||||
def resizable(self, *args, **kwargs):
|
||||
super().resizable(*args, **kwargs)
|
||||
self.last_resizable_args = (args, kwargs)
|
||||
self._last_resizable_args = (args, kwargs)
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if self.appearance_mode == 1:
|
||||
self.windows_set_titlebar_color("dark")
|
||||
if self._appearance_mode == 1:
|
||||
self._windows_set_titlebar_color("dark")
|
||||
else:
|
||||
self.windows_set_titlebar_color("light")
|
||||
self._windows_set_titlebar_color("light")
|
||||
|
||||
def minsize(self, width=None, height=None):
|
||||
self.min_width = width
|
||||
self.min_height = height
|
||||
if self.current_width < width: self.current_width = width
|
||||
if self.current_height < height: self.current_height = height
|
||||
super().minsize(self.apply_window_scaling(self.min_width), self.apply_window_scaling(self.min_height))
|
||||
self._min_width = width
|
||||
self._min_height = height
|
||||
if self._current_width < width:
|
||||
self._current_width = width
|
||||
if self._current_height < height:
|
||||
self._current_height = height
|
||||
super().minsize(self._apply_window_scaling(self._min_width), self._apply_window_scaling(self._min_height))
|
||||
|
||||
def maxsize(self, width=None, height=None):
|
||||
self.max_width = width
|
||||
self.max_height = height
|
||||
if self.current_width > width: self.current_width = width
|
||||
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))
|
||||
self._max_width = width
|
||||
self._max_height = height
|
||||
if self._current_width > width:
|
||||
self._current_width = width
|
||||
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: str = None):
|
||||
if geometry_string is not None:
|
||||
super().geometry(self.apply_geometry_scaling(geometry_string))
|
||||
super().geometry(self._apply_geometry_scaling(geometry_string))
|
||||
|
||||
# update width and height attributes
|
||||
width, height, x, y = self.parse_geometry_string(geometry_string)
|
||||
width, height, x, y = self._parse_geometry_string(geometry_string)
|
||||
if width is not None and height is not None:
|
||||
self.current_width = max(self.min_width, min(width, self.max_width)) # bound value between min and max
|
||||
self.current_height = max(self.min_height, min(height, self.max_height))
|
||||
self._current_width = max(self._min_width, min(width, self._max_width)) # bound value between min and max
|
||||
self._current_height = max(self._min_height, min(height, self._max_height))
|
||||
else:
|
||||
return self.reverse_geometry_scaling(super().geometry())
|
||||
|
||||
@staticmethod
|
||||
def parse_geometry_string(geometry_string: str) -> tuple:
|
||||
def _parse_geometry_string(geometry_string: str) -> tuple:
|
||||
# index: 1 2 3 4 5 6
|
||||
# regex group structure: ('<width>x<height>', '<width>', '<height>', '+-<x>+-<y>', '-<x>', '-<y>')
|
||||
result = re.search(r"((\d+)x(\d+)){0,1}(\+{0,1}([+-]{0,1}\d+)\+{0,1}([+-]{0,1}\d+)){0,1}", geometry_string)
|
||||
@ -188,33 +195,33 @@ class CTk(tkinter.Tk):
|
||||
|
||||
return width, height, x, y
|
||||
|
||||
def apply_geometry_scaling(self, geometry_string: str) -> str:
|
||||
width, height, x, y = self.parse_geometry_string(geometry_string)
|
||||
def _apply_geometry_scaling(self, geometry_string: str) -> str:
|
||||
width, height, x, y = self._parse_geometry_string(geometry_string)
|
||||
|
||||
if x is None and y is None: # no <x> and <y> in geometry_string
|
||||
return f"{round(width * self.window_scaling)}x{round(height * self.window_scaling)}"
|
||||
return f"{round(width * self._window_scaling)}x{round(height * self._window_scaling)}"
|
||||
|
||||
elif width is None and height is None: # no <width> and <height> in geometry_string
|
||||
return f"+{x}+{y}"
|
||||
|
||||
else:
|
||||
return f"{round(width * self.window_scaling)}x{round(height * self.window_scaling)}+{x}+{y}"
|
||||
return f"{round(width * self._window_scaling)}x{round(height * self._window_scaling)}+{x}+{y}"
|
||||
|
||||
def reverse_geometry_scaling(self, scaled_geometry_string: str) -> str:
|
||||
width, height, x, y = self.parse_geometry_string(scaled_geometry_string)
|
||||
def _reverse_geometry_scaling(self, scaled_geometry_string: str) -> str:
|
||||
width, height, x, y = self._parse_geometry_string(scaled_geometry_string)
|
||||
|
||||
if x is None and y is None: # no <x> and <y> in geometry_string
|
||||
return f"{round(width / self.window_scaling)}x{round(height / self.window_scaling)}"
|
||||
return f"{round(width / self._window_scaling)}x{round(height / self._window_scaling)}"
|
||||
|
||||
elif width is None and height is None: # no <width> and <height> in geometry_string
|
||||
return f"+{x}+{y}"
|
||||
|
||||
else:
|
||||
return f"{round(width / self.window_scaling)}x{round(height / self.window_scaling)}+{x}+{y}"
|
||||
return f"{round(width / self._window_scaling)}x{round(height / self._window_scaling)}+{x}+{y}"
|
||||
|
||||
def apply_window_scaling(self, value):
|
||||
def _apply_window_scaling(self, value):
|
||||
if isinstance(value, (int, float)):
|
||||
return int(value * self.window_scaling)
|
||||
return int(value * self._window_scaling)
|
||||
else:
|
||||
return value
|
||||
|
||||
@ -225,26 +232,25 @@ class CTk(tkinter.Tk):
|
||||
bg_changed = False
|
||||
|
||||
if "bg" in kwargs:
|
||||
self.fg_color = kwargs["bg"]
|
||||
self._fg_color = kwargs["bg"]
|
||||
bg_changed = True
|
||||
kwargs["bg"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
kwargs["bg"] = ThemeManager.single_color(self._fg_color, self._appearance_mode)
|
||||
elif "background" in kwargs:
|
||||
self.fg_color = kwargs["background"]
|
||||
self._fg_color = kwargs["background"]
|
||||
bg_changed = True
|
||||
kwargs["background"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
kwargs["background"] = ThemeManager.single_color(self._fg_color, self._appearance_mode)
|
||||
elif "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
kwargs["bg"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
del kwargs["fg_color"]
|
||||
self._fg_color = kwargs.pop("fg_color")
|
||||
kwargs["bg"] = ThemeManager.single_color(self._fg_color, self._appearance_mode)
|
||||
bg_changed = True
|
||||
|
||||
elif len(args) > 0 and type(args[0]) == dict:
|
||||
if "bg" in args[0]:
|
||||
self.fg_color=args[0]["bg"]
|
||||
self._fg_color = args[0]["bg"]
|
||||
bg_changed = True
|
||||
args[0]["bg"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
elif "background" in args[0]:
|
||||
self.fg_color=args[0]["background"]
|
||||
self._fg_color = args[0]["background"]
|
||||
bg_changed = True
|
||||
args[0]["background"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
|
||||
@ -252,13 +258,13 @@ class CTk(tkinter.Tk):
|
||||
from ..widgets.widget_base_class import CTkBaseClass
|
||||
|
||||
for child in self.winfo_children():
|
||||
if isinstance(child, CTkBaseClass):
|
||||
child.configure(bg_color=self.fg_color)
|
||||
if isinstance(child, CTkBaseClass) and hasattr(child, "_fg_color"):
|
||||
child.configure(bg_color=self._fg_color)
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def enable_macos_dark_title_bar():
|
||||
def _enable_macos_dark_title_bar():
|
||||
if sys.platform == "darwin" and not Settings.deactivate_macos_window_header_manipulation: # macOS
|
||||
if Version(platform.python_version()) < Version("3.10"):
|
||||
if Version(tkinter.Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
|
||||
@ -266,14 +272,14 @@ class CTk(tkinter.Tk):
|
||||
# This command allows dark-mode for all programs
|
||||
|
||||
@staticmethod
|
||||
def disable_macos_dark_title_bar():
|
||||
def _disable_macos_dark_title_bar():
|
||||
if sys.platform == "darwin" and not Settings.deactivate_macos_window_header_manipulation: # macOS
|
||||
if Version(platform.python_version()) < Version("3.10"):
|
||||
if Version(tkinter.Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
|
||||
os.system("defaults delete -g NSRequiresAquaSystemAppearance")
|
||||
# This command reverts the dark-mode setting for all programs.
|
||||
|
||||
def windows_set_titlebar_color(self, color_mode: str):
|
||||
def _windows_set_titlebar_color(self, color_mode: str):
|
||||
"""
|
||||
Set the titlebar color of the window to light or dark theme on Microsoft Windows.
|
||||
|
||||
@ -286,11 +292,11 @@ class CTk(tkinter.Tk):
|
||||
|
||||
if sys.platform.startswith("win") and not Settings.deactivate_windows_window_header_manipulation:
|
||||
|
||||
if self.window_exists:
|
||||
self.state_before_windows_set_titlebar_color = self.state()
|
||||
if self._window_exists:
|
||||
self._state_before_windows_set_titlebar_color = self.state()
|
||||
# print("window_exists -> state_before_windows_set_titlebar_color: ", self.state_before_windows_set_titlebar_color)
|
||||
|
||||
if self.state_before_windows_set_titlebar_color != "iconic" or self.state_before_windows_set_titlebar_color != "withdrawn":
|
||||
if self._state_before_windows_set_titlebar_color != "iconic" or self._state_before_windows_set_titlebar_color != "withdrawn":
|
||||
super().withdraw() # hide window so that it can be redrawn after the titlebar change so that the color change is visible
|
||||
else:
|
||||
# print("window dont exists -> withdraw and update")
|
||||
@ -322,29 +328,29 @@ class CTk(tkinter.Tk):
|
||||
except Exception as err:
|
||||
print(err)
|
||||
|
||||
if self.window_exists:
|
||||
if self._window_exists:
|
||||
# print("window_exists -> return to original state: ", self.state_before_windows_set_titlebar_color)
|
||||
if self.state_before_windows_set_titlebar_color == "normal":
|
||||
if self._state_before_windows_set_titlebar_color == "normal":
|
||||
self.deiconify()
|
||||
elif self.state_before_windows_set_titlebar_color == "iconic":
|
||||
elif self._state_before_windows_set_titlebar_color == "iconic":
|
||||
self.iconify()
|
||||
elif self.state_before_windows_set_titlebar_color == "zoomed":
|
||||
elif self._state_before_windows_set_titlebar_color == "zoomed":
|
||||
self.state("zoomed")
|
||||
else:
|
||||
self.state(self.state_before_windows_set_titlebar_color) # other states
|
||||
self.state(self._state_before_windows_set_titlebar_color) # other states
|
||||
else:
|
||||
pass # wait for update or mainloop to be called
|
||||
|
||||
def set_appearance_mode(self, mode_string):
|
||||
def _set_appearance_mode(self, mode_string):
|
||||
if mode_string.lower() == "dark":
|
||||
self.appearance_mode = 1
|
||||
self._appearance_mode = 1
|
||||
elif mode_string.lower() == "light":
|
||||
self.appearance_mode = 0
|
||||
self._appearance_mode = 0
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if self.appearance_mode == 1:
|
||||
self.windows_set_titlebar_color("dark")
|
||||
if self._appearance_mode == 1:
|
||||
self._windows_set_titlebar_color("dark")
|
||||
else:
|
||||
self.windows_set_titlebar_color("light")
|
||||
self._windows_set_titlebar_color("light")
|
||||
|
||||
super().configure(bg=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
super().configure(bg=ThemeManager.single_color(self._fg_color, self._appearance_mode))
|
||||
|
@ -14,93 +14,96 @@ from ..scaling_tracker import ScalingTracker
|
||||
|
||||
|
||||
class CTkToplevel(tkinter.Toplevel):
|
||||
"""
|
||||
Toplevel window with dark titlebar on Windows and macOS.
|
||||
For detailed information check out the documentation.
|
||||
"""
|
||||
|
||||
def __init__(self, *args,
|
||||
fg_color="default_theme",
|
||||
fg_color: Union[str, Tuple[str, str]] = "default_theme",
|
||||
**kwargs):
|
||||
|
||||
self.enable_macos_dark_title_bar()
|
||||
self._enable_macos_dark_title_bar()
|
||||
super().__init__(*args, **kwargs)
|
||||
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
self._appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
|
||||
# add set_scaling method to callback list of ScalingTracker for automatic scaling changes
|
||||
ScalingTracker.add_widget(self.set_scaling, self)
|
||||
self.window_scaling = ScalingTracker.get_window_scaling(self)
|
||||
ScalingTracker.add_widget(self._set_scaling, self)
|
||||
self._window_scaling = ScalingTracker.get_window_scaling(self)
|
||||
|
||||
self.current_width = 200 # initial window size, always without scaling
|
||||
self.current_height = 200
|
||||
self.min_width: int = 0
|
||||
self.min_height: int = 0
|
||||
self.max_width: int = 1_000_000
|
||||
self.max_height: int = 1_000_000
|
||||
self.last_resizable_args: Union[Tuple[list, dict], None] = None # (args, kwargs)
|
||||
self._current_width = 200 # initial window size, always without scaling
|
||||
self._current_height = 200
|
||||
self._min_width: int = 0
|
||||
self._min_height: int = 0
|
||||
self._max_width: int = 1_000_000
|
||||
self._max_height: int = 1_000_000
|
||||
self._last_resizable_args: Union[Tuple[list, dict], None] = None # (args, kwargs)
|
||||
|
||||
self.fg_color = ThemeManager.theme["color"]["window_bg_color"] if fg_color == "default_theme" else fg_color
|
||||
self._fg_color = ThemeManager.theme["color"]["window_bg_color"] if fg_color == "default_theme" else fg_color
|
||||
|
||||
if "bg" in kwargs:
|
||||
self.fg_color = kwargs["bg"]
|
||||
del kwargs["bg"]
|
||||
self._fg_color = kwargs.pop("bg")
|
||||
elif "background" in kwargs:
|
||||
self.fg_color = kwargs["background"]
|
||||
del kwargs["background"]
|
||||
self.fg_color = kwargs.pop("background")
|
||||
|
||||
# add set_appearance_mode method to callback list of AppearanceModeTracker for appearance mode changes
|
||||
AppearanceModeTracker.add(self.set_appearance_mode, self)
|
||||
super().configure(bg=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
AppearanceModeTracker.add(self._set_appearance_mode, self)
|
||||
super().configure(bg=ThemeManager.single_color(self._fg_color, self._appearance_mode))
|
||||
super().title("CTkToplevel")
|
||||
|
||||
self.state_before_windows_set_titlebar_color = None
|
||||
self.windows_set_titlebar_color_called = False # indicates if windows_set_titlebar_color was called, stays True until revert_withdraw_after_windows_set_titlebar_color is called
|
||||
self.withdraw_called_after_windows_set_titlebar_color = False # indicates if withdraw() was called after windows_set_titlebar_color
|
||||
self.iconify_called_after_windows_set_titlebar_color = False # indicates if iconify() was called after windows_set_titlebar_color
|
||||
self._state_before_windows_set_titlebar_color = None
|
||||
self._windows_set_titlebar_color_called = False # indicates if windows_set_titlebar_color was called, stays True until revert_withdraw_after_windows_set_titlebar_color is called
|
||||
self._withdraw_called_after_windows_set_titlebar_color = False # indicates if withdraw() was called after windows_set_titlebar_color
|
||||
self._iconify_called_after_windows_set_titlebar_color = False # indicates if iconify() was called after windows_set_titlebar_color
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if self.appearance_mode == 1:
|
||||
self.windows_set_titlebar_color("dark")
|
||||
if self._appearance_mode == 1:
|
||||
self._windows_set_titlebar_color("dark")
|
||||
else:
|
||||
self.windows_set_titlebar_color("light")
|
||||
self._windows_set_titlebar_color("light")
|
||||
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
self.bind('<Configure>', self._update_dimensions_event)
|
||||
|
||||
def update_dimensions_event(self, event=None):
|
||||
def _update_dimensions_event(self, event=None):
|
||||
detected_width = self.winfo_width() # detect current window size
|
||||
detected_height = self.winfo_height()
|
||||
|
||||
if self.current_width != round(detected_width / self.window_scaling) or self.current_height != round(detected_height / self.window_scaling):
|
||||
self.current_width = round(detected_width / self.window_scaling) # adjust current size according to new size given by event
|
||||
self.current_height = round(detected_height / self.window_scaling) # _current_width and _current_height are independent of the scale
|
||||
if self._current_width != round(detected_width / self._window_scaling) or self._current_height != round(detected_height / self._window_scaling):
|
||||
self._current_width = round(detected_width / self._window_scaling) # adjust current size according to new size given by event
|
||||
self._current_height = round(detected_height / self._window_scaling) # _current_width and _current_height are independent of the scale
|
||||
|
||||
def set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
self.window_scaling = new_window_scaling
|
||||
def _set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
self._window_scaling = new_window_scaling
|
||||
|
||||
# force new dimensions on window by using min, max, and geometry
|
||||
super().minsize(self.apply_window_scaling(self.current_width), self.apply_window_scaling(self.current_height))
|
||||
super().maxsize(self.apply_window_scaling(self.current_width), self.apply_window_scaling(self.current_height))
|
||||
super().minsize(self._apply_window_scaling(self._current_width), self._apply_window_scaling(self._current_height))
|
||||
super().maxsize(self._apply_window_scaling(self._current_width), self._apply_window_scaling(self._current_height))
|
||||
super().geometry(
|
||||
f"{self.apply_window_scaling(self.current_width)}x" + f"{self.apply_window_scaling(self.current_height)}")
|
||||
f"{self._apply_window_scaling(self._current_width)}x" + f"{self._apply_window_scaling(self._current_height)}")
|
||||
|
||||
# set new scaled min and max with 400ms delay (otherwise it won't work for some reason)
|
||||
self.after(400, self.set_scaled_min_max)
|
||||
self.after(400, self._set_scaled_min_max)
|
||||
|
||||
def set_scaled_min_max(self):
|
||||
if self.min_width is not None or self.min_height is not None:
|
||||
super().minsize(self.apply_window_scaling(self.min_width), self.apply_window_scaling(self.min_height))
|
||||
if self.max_width is not None or self.max_height is not None:
|
||||
super().maxsize(self.apply_window_scaling(self.max_width), self.apply_window_scaling(self.max_height))
|
||||
def _set_scaled_min_max(self):
|
||||
if self._min_width is not None or self._min_height is not None:
|
||||
super().minsize(self._apply_window_scaling(self._min_width), self._apply_window_scaling(self._min_height))
|
||||
if self._max_width is not None or self._max_height is not None:
|
||||
super().maxsize(self._apply_window_scaling(self._max_width), self._apply_window_scaling(self._max_height))
|
||||
|
||||
def geometry(self, geometry_string: str = None):
|
||||
if geometry_string is not None:
|
||||
super().geometry(self.apply_geometry_scaling(geometry_string))
|
||||
super().geometry(self._apply_geometry_scaling(geometry_string))
|
||||
|
||||
# update width and height attributes
|
||||
width, height, x, y = self.parse_geometry_string(geometry_string)
|
||||
width, height, x, y = self._parse_geometry_string(geometry_string)
|
||||
if width is not None and height is not None:
|
||||
self.current_width = max(self.min_width, min(width, self.max_width)) # bound value between min and max
|
||||
self.current_height = max(self.min_height, min(height, self.max_height))
|
||||
self._current_width = max(self._min_width, min(width, self._max_width)) # bound value between min and max
|
||||
self._current_height = max(self._min_height, min(height, self._max_height))
|
||||
else:
|
||||
return self.reverse_geometry_scaling(super().geometry())
|
||||
return self._reverse_geometry_scaling(super().geometry())
|
||||
|
||||
@staticmethod
|
||||
def parse_geometry_string(geometry_string: str) -> tuple:
|
||||
def _parse_geometry_string(geometry_string: str) -> tuple:
|
||||
# index: 1 2 3 4 5 6
|
||||
# regex group structure: ('<width>x<height>', '<width>', '<height>', '+-<x>+-<y>', '-<x>', '-<y>')
|
||||
result = re.search(r"((\d+)x(\d+)){0,1}(\+{0,1}([+-]{0,1}\d+)\+{0,1}([+-]{0,1}\d+)){0,1}", geometry_string)
|
||||
@ -112,75 +115,79 @@ class CTkToplevel(tkinter.Toplevel):
|
||||
|
||||
return width, height, x, y
|
||||
|
||||
def apply_geometry_scaling(self, geometry_string: str) -> str:
|
||||
width, height, x, y = self.parse_geometry_string(geometry_string)
|
||||
def _apply_geometry_scaling(self, geometry_string: str) -> str:
|
||||
width, height, x, y = self._parse_geometry_string(geometry_string)
|
||||
|
||||
if x is None and y is None: # no <x> and <y> in geometry_string
|
||||
return f"{round(width * self.window_scaling)}x{round(height * self.window_scaling)}"
|
||||
return f"{round(width * self._window_scaling)}x{round(height * self._window_scaling)}"
|
||||
|
||||
elif width is None and height is None: # no <width> and <height> in geometry_string
|
||||
return f"+{x}+{y}"
|
||||
|
||||
else:
|
||||
return f"{round(width * self.window_scaling)}x{round(height * self.window_scaling)}+{x}+{y}"
|
||||
return f"{round(width * self._window_scaling)}x{round(height * self._window_scaling)}+{x}+{y}"
|
||||
|
||||
def reverse_geometry_scaling(self, scaled_geometry_string: str) -> str:
|
||||
width, height, x, y = self.parse_geometry_string(scaled_geometry_string)
|
||||
def _reverse_geometry_scaling(self, scaled_geometry_string: str) -> str:
|
||||
width, height, x, y = self._parse_geometry_string(scaled_geometry_string)
|
||||
|
||||
if x is None and y is None: # no <x> and <y> in geometry_string
|
||||
return f"{round(width / self.window_scaling)}x{round(height / self.window_scaling)}"
|
||||
return f"{round(width / self._window_scaling)}x{round(height / self._window_scaling)}"
|
||||
|
||||
elif width is None and height is None: # no <width> and <height> in geometry_string
|
||||
return f"+{x}+{y}"
|
||||
|
||||
else:
|
||||
return f"{round(width / self.window_scaling)}x{round(height / self.window_scaling)}+{x}+{y}"
|
||||
return f"{round(width / self._window_scaling)}x{round(height / self._window_scaling)}+{x}+{y}"
|
||||
|
||||
def apply_window_scaling(self, value):
|
||||
def _apply_window_scaling(self, value):
|
||||
if isinstance(value, (int, float)):
|
||||
return int(value * self.window_scaling)
|
||||
return int(value * self._window_scaling)
|
||||
else:
|
||||
return value
|
||||
|
||||
def destroy(self):
|
||||
AppearanceModeTracker.remove(self.set_appearance_mode)
|
||||
ScalingTracker.remove_window(self.set_scaling, self)
|
||||
self.disable_macos_dark_title_bar()
|
||||
AppearanceModeTracker.remove(self._set_appearance_mode)
|
||||
ScalingTracker.remove_window(self._set_scaling, self)
|
||||
self._disable_macos_dark_title_bar()
|
||||
super().destroy()
|
||||
|
||||
def withdraw(self):
|
||||
if self.windows_set_titlebar_color_called:
|
||||
self.withdraw_called_after_windows_set_titlebar_color = True
|
||||
if self._windows_set_titlebar_color_called:
|
||||
self._withdraw_called_after_windows_set_titlebar_color = True
|
||||
super().withdraw()
|
||||
|
||||
def iconify(self):
|
||||
if self.windows_set_titlebar_color_called:
|
||||
self.iconify_called_after_windows_set_titlebar_color = True
|
||||
if self._windows_set_titlebar_color_called:
|
||||
self._iconify_called_after_windows_set_titlebar_color = True
|
||||
super().iconify()
|
||||
|
||||
def resizable(self, *args, **kwargs):
|
||||
super().resizable(*args, **kwargs)
|
||||
self.last_resizable_args = (args, kwargs)
|
||||
self._last_resizable_args = (args, kwargs)
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if self.appearance_mode == 1:
|
||||
self.after(10, lambda: self.windows_set_titlebar_color("dark"))
|
||||
if self._appearance_mode == 1:
|
||||
self.after(10, lambda: self._windows_set_titlebar_color("dark"))
|
||||
else:
|
||||
self.after(10, lambda: self.windows_set_titlebar_color("light"))
|
||||
self.after(10, lambda: self._windows_set_titlebar_color("light"))
|
||||
|
||||
def minsize(self, width=None, height=None):
|
||||
self.min_width = width
|
||||
self.min_height = height
|
||||
if self.current_width < width: self.current_width = width
|
||||
if self.current_height < height: self.current_height = height
|
||||
super().minsize(self.apply_window_scaling(self.min_width), self.apply_window_scaling(self.min_height))
|
||||
self._min_width = width
|
||||
self._min_height = height
|
||||
if self._current_width < width:
|
||||
self._current_width = width
|
||||
if self._current_height < height:
|
||||
self._current_height = height
|
||||
super().minsize(self._apply_window_scaling(self._min_width), self._apply_window_scaling(self._min_height))
|
||||
|
||||
def maxsize(self, width=None, height=None):
|
||||
self.max_width = width
|
||||
self.max_height = height
|
||||
if self.current_width > width: self.current_width = width
|
||||
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))
|
||||
self._max_width = width
|
||||
self._max_height = height
|
||||
if self._current_width > width:
|
||||
self._current_width = width
|
||||
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 config(self, *args, **kwargs):
|
||||
self.configure(*args, **kwargs)
|
||||
@ -189,54 +196,53 @@ class CTkToplevel(tkinter.Toplevel):
|
||||
bg_changed = False
|
||||
|
||||
if "bg" in kwargs:
|
||||
self.fg_color = kwargs["bg"]
|
||||
self._fg_color = kwargs["bg"]
|
||||
bg_changed = True
|
||||
kwargs["bg"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
kwargs["bg"] = ThemeManager.single_color(self._fg_color, self._appearance_mode)
|
||||
elif "background" in kwargs:
|
||||
self.fg_color = kwargs["background"]
|
||||
self._fg_color = kwargs["background"]
|
||||
bg_changed = True
|
||||
kwargs["background"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
kwargs["background"] = ThemeManager.single_color(self._fg_color, self._appearance_mode)
|
||||
elif "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
kwargs["bg"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
del kwargs["fg_color"]
|
||||
self._fg_color = kwargs.pop("fg_color")
|
||||
kwargs["bg"] = ThemeManager.single_color(self._fg_color, self._appearance_mode)
|
||||
bg_changed = True
|
||||
|
||||
elif len(args) > 0 and type(args[0]) == dict:
|
||||
if "bg" in args[0]:
|
||||
self.fg_color=args[0]["bg"]
|
||||
self._fg_color = args[0]["bg"]
|
||||
bg_changed = True
|
||||
args[0]["bg"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
args[0]["bg"] = ThemeManager.single_color(self._fg_color, self._appearance_mode)
|
||||
elif "background" in args[0]:
|
||||
self.fg_color=args[0]["background"]
|
||||
self._fg_color = args[0]["background"]
|
||||
bg_changed = True
|
||||
args[0]["background"] = ThemeManager.single_color(self.fg_color, self.appearance_mode)
|
||||
args[0]["background"] = ThemeManager.single_color(self._fg_color, self._appearance_mode)
|
||||
|
||||
if bg_changed:
|
||||
from ..widgets.widget_base_class import CTkBaseClass
|
||||
|
||||
for child in self.winfo_children():
|
||||
if isinstance(child, CTkBaseClass):
|
||||
child.configure(bg_color=self.fg_color)
|
||||
if isinstance(child, CTkBaseClass) and hasattr(child, "_fg_color"):
|
||||
child.configure(bg_color=self._fg_color)
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def enable_macos_dark_title_bar():
|
||||
def _enable_macos_dark_title_bar():
|
||||
if sys.platform == "darwin" and not Settings.deactivate_macos_window_header_manipulation: # macOS
|
||||
if Version(platform.python_version()) < Version("3.10"):
|
||||
if Version(tkinter.Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
|
||||
os.system("defaults write -g NSRequiresAquaSystemAppearance -bool No")
|
||||
|
||||
@staticmethod
|
||||
def disable_macos_dark_title_bar():
|
||||
def _disable_macos_dark_title_bar():
|
||||
if sys.platform == "darwin" and not Settings.deactivate_macos_window_header_manipulation: # macOS
|
||||
if Version(platform.python_version()) < Version("3.10"):
|
||||
if Version(tkinter.Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
|
||||
os.system("defaults delete -g NSRequiresAquaSystemAppearance")
|
||||
# This command reverts the dark-mode setting for all programs.
|
||||
|
||||
def windows_set_titlebar_color(self, color_mode: str):
|
||||
def _windows_set_titlebar_color(self, color_mode: str):
|
||||
"""
|
||||
Set the titlebar color of the window to light or dark theme on Microsoft Windows.
|
||||
|
||||
@ -249,7 +255,7 @@ class CTkToplevel(tkinter.Toplevel):
|
||||
|
||||
if sys.platform.startswith("win") and not Settings.deactivate_windows_window_header_manipulation:
|
||||
|
||||
self.state_before_windows_set_titlebar_color = self.state()
|
||||
self._state_before_windows_set_titlebar_color = self.state()
|
||||
super().withdraw() # hide window so that it can be redrawn after the titlebar change so that the color change is visible
|
||||
super().update()
|
||||
|
||||
@ -277,41 +283,41 @@ class CTkToplevel(tkinter.Toplevel):
|
||||
except Exception as err:
|
||||
print(err)
|
||||
|
||||
self.windows_set_titlebar_color_called = True
|
||||
self.after(5, self.revert_withdraw_after_windows_set_titlebar_color)
|
||||
self._windows_set_titlebar_color_called = True
|
||||
self.after(5, self._revert_withdraw_after_windows_set_titlebar_color)
|
||||
|
||||
def revert_withdraw_after_windows_set_titlebar_color(self):
|
||||
def _revert_withdraw_after_windows_set_titlebar_color(self):
|
||||
""" if in a short time (5ms) after """
|
||||
if self.windows_set_titlebar_color_called:
|
||||
if self._windows_set_titlebar_color_called:
|
||||
|
||||
if self.withdraw_called_after_windows_set_titlebar_color:
|
||||
if self._withdraw_called_after_windows_set_titlebar_color:
|
||||
pass # leave it withdrawed
|
||||
elif self.iconify_called_after_windows_set_titlebar_color:
|
||||
elif self._iconify_called_after_windows_set_titlebar_color:
|
||||
super().iconify()
|
||||
else:
|
||||
if self.state_before_windows_set_titlebar_color == "normal":
|
||||
if self._state_before_windows_set_titlebar_color == "normal":
|
||||
self.deiconify()
|
||||
elif self.state_before_windows_set_titlebar_color == "iconic":
|
||||
elif self._state_before_windows_set_titlebar_color == "iconic":
|
||||
self.iconify()
|
||||
elif self.state_before_windows_set_titlebar_color == "zoomed":
|
||||
elif self._state_before_windows_set_titlebar_color == "zoomed":
|
||||
self.state("zoomed")
|
||||
else:
|
||||
self.state(self.state_before_windows_set_titlebar_color) # other states
|
||||
self.state(self._state_before_windows_set_titlebar_color) # other states
|
||||
|
||||
self.windows_set_titlebar_color_called = False
|
||||
self.withdraw_called_after_windows_set_titlebar_color = False
|
||||
self.iconify_called_after_windows_set_titlebar_color = False
|
||||
self._windows_set_titlebar_color_called = False
|
||||
self._withdraw_called_after_windows_set_titlebar_color = False
|
||||
self._iconify_called_after_windows_set_titlebar_color = False
|
||||
|
||||
def set_appearance_mode(self, mode_string):
|
||||
def _set_appearance_mode(self, mode_string):
|
||||
if mode_string.lower() == "dark":
|
||||
self.appearance_mode = 1
|
||||
self._appearance_mode = 1
|
||||
elif mode_string.lower() == "light":
|
||||
self.appearance_mode = 0
|
||||
self._appearance_mode = 0
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if self.appearance_mode == 1:
|
||||
self.windows_set_titlebar_color("dark")
|
||||
if self._appearance_mode == 1:
|
||||
self._windows_set_titlebar_color("dark")
|
||||
else:
|
||||
self.windows_set_titlebar_color("light")
|
||||
self._windows_set_titlebar_color("light")
|
||||
|
||||
super().configure(bg=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
super().configure(bg=ThemeManager.single_color(self._fg_color, self._appearance_mode))
|
||||
|
Reference in New Issue
Block a user