2022-04-19 01:01:33 +03:00
|
|
|
import tkinter
|
|
|
|
import sys
|
2022-05-22 01:02:45 +03:00
|
|
|
from typing import Callable
|
2022-05-05 21:25:37 +03:00
|
|
|
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
class ScalingTracker:
|
2022-05-22 01:02:45 +03:00
|
|
|
deactivate_automatic_dpi_awareness = False
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
window_widgets_dict = {} # contains window objects as keys with list of widget callbacks as elements
|
2022-05-02 00:29:14 +03:00
|
|
|
window_dpi_scaling_dict = {} # contains window objects as keys and corresponding scaling factors
|
|
|
|
|
|
|
|
widget_scaling = 1 # user values which multiply to detected window scaling factor
|
|
|
|
window_scaling = 1
|
|
|
|
spacing_scaling = 1
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
update_loop_running = False
|
2022-05-22 02:55:58 +03:00
|
|
|
update_loop_interval = 150 # milliseconds
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def get_widget_scaling(cls, widget) -> float:
|
2022-04-19 01:01:33 +03:00
|
|
|
window_root = cls.get_window_root_of_widget(widget)
|
2022-05-02 00:29:14 +03:00
|
|
|
return cls.window_dpi_scaling_dict[window_root] * cls.widget_scaling
|
|
|
|
|
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def get_spacing_scaling(cls, widget) -> float:
|
2022-05-02 00:29:14 +03:00
|
|
|
window_root = cls.get_window_root_of_widget(widget)
|
|
|
|
return cls.window_dpi_scaling_dict[window_root] * cls.spacing_scaling
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def get_window_scaling(cls, window) -> float:
|
2022-05-02 00:29:14 +03:00
|
|
|
window_root = cls.get_window_root_of_widget(window)
|
|
|
|
return cls.window_dpi_scaling_dict[window_root] * cls.window_scaling
|
|
|
|
|
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def set_widget_scaling(cls, widget_scaling_factor: float):
|
2022-05-06 15:33:38 +03:00
|
|
|
cls.widget_scaling = max(widget_scaling_factor, 0.4)
|
2022-05-22 01:02:45 +03:00
|
|
|
cls.update_scaling_callbacks_all()
|
2022-05-02 00:29:14 +03:00
|
|
|
|
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def set_spacing_scaling(cls, spacing_scaling_factor: float):
|
2022-05-06 15:33:38 +03:00
|
|
|
cls.spacing_scaling = max(spacing_scaling_factor, 0.4)
|
2022-05-22 01:02:45 +03:00
|
|
|
cls.update_scaling_callbacks_all()
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def set_window_scaling(cls, window_scaling_factor: float):
|
2022-05-06 15:33:38 +03:00
|
|
|
cls.window_scaling = max(window_scaling_factor, 0.4)
|
2022-05-22 01:02:45 +03:00
|
|
|
cls.update_scaling_callbacks_all()
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_window_root_of_widget(cls, widget):
|
|
|
|
current_widget = widget
|
|
|
|
|
|
|
|
while isinstance(current_widget, tkinter.Tk) is False and\
|
|
|
|
isinstance(current_widget, tkinter.Toplevel) is False:
|
|
|
|
current_widget = current_widget.master
|
|
|
|
|
|
|
|
return current_widget
|
|
|
|
|
|
|
|
@classmethod
|
2022-05-22 01:02:45 +03:00
|
|
|
def update_scaling_callbacks_all(cls):
|
2022-04-19 01:01:33 +03:00
|
|
|
for window, callback_list in cls.window_widgets_dict.items():
|
2022-05-22 01:02:45 +03:00
|
|
|
for set_scaling_callback in callback_list:
|
|
|
|
if not cls.deactivate_automatic_dpi_awareness:
|
|
|
|
set_scaling_callback(cls.window_dpi_scaling_dict[window] * cls.widget_scaling,
|
|
|
|
cls.window_dpi_scaling_dict[window] * cls.spacing_scaling,
|
|
|
|
cls.window_dpi_scaling_dict[window] * cls.window_scaling)
|
2022-05-05 21:25:37 +03:00
|
|
|
else:
|
2022-05-22 01:02:45 +03:00
|
|
|
set_scaling_callback(cls.widget_scaling,
|
|
|
|
cls.spacing_scaling,
|
|
|
|
cls.window_scaling)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def update_scaling_callbacks_for_window(cls, window):
|
|
|
|
for set_scaling_callback in cls.window_widgets_dict[window]:
|
|
|
|
if not cls.deactivate_automatic_dpi_awareness:
|
|
|
|
set_scaling_callback(cls.window_dpi_scaling_dict[window] * cls.widget_scaling,
|
|
|
|
cls.window_dpi_scaling_dict[window] * cls.spacing_scaling,
|
|
|
|
cls.window_dpi_scaling_dict[window] * cls.window_scaling)
|
|
|
|
else:
|
|
|
|
set_scaling_callback(cls.widget_scaling,
|
|
|
|
cls.spacing_scaling,
|
|
|
|
cls.window_scaling)
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def add_widget(cls, widget_callback: Callable, widget):
|
2022-04-19 01:01:33 +03:00
|
|
|
window_root = cls.get_window_root_of_widget(widget)
|
|
|
|
|
|
|
|
if window_root not in cls.window_widgets_dict:
|
|
|
|
cls.window_widgets_dict[window_root] = [widget_callback]
|
|
|
|
else:
|
|
|
|
cls.window_widgets_dict[window_root].append(widget_callback)
|
|
|
|
|
|
|
|
if window_root not in cls.window_dpi_scaling_dict:
|
2022-05-02 00:29:14 +03:00
|
|
|
cls.window_dpi_scaling_dict[window_root] = cls.get_window_dpi_scaling(window_root)
|
2022-04-19 01:01:33 +03:00
|
|
|
|
2022-05-22 01:02:45 +03:00
|
|
|
if not cls.update_loop_running:
|
|
|
|
window_root.after(100, cls.check_dpi_scaling)
|
|
|
|
cls.update_loop_running = True
|
2022-05-16 17:51:19 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def remove_widget(cls, widget_callback, widget):
|
|
|
|
window_root = cls.get_window_root_of_widget(widget)
|
|
|
|
try:
|
|
|
|
cls.window_widgets_dict[window_root].remove(widget_callback)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def remove_window(cls, window_callback, window):
|
|
|
|
try:
|
|
|
|
del cls.window_widgets_dict[window]
|
|
|
|
except:
|
|
|
|
pass
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def add_window(cls, window_callback, window):
|
|
|
|
if window not in cls.window_widgets_dict:
|
|
|
|
cls.window_widgets_dict[window] = [window_callback]
|
|
|
|
else:
|
|
|
|
cls.window_widgets_dict[window].append(window_callback)
|
|
|
|
|
|
|
|
if window not in cls.window_dpi_scaling_dict:
|
2022-05-02 00:29:14 +03:00
|
|
|
cls.window_dpi_scaling_dict[window] = cls.get_window_dpi_scaling(window)
|
2022-04-19 01:01:33 +03:00
|
|
|
|
2022-05-05 21:25:37 +03:00
|
|
|
@classmethod
|
|
|
|
def activate_high_dpi_awareness(cls):
|
2022-05-26 20:16:01 +03:00
|
|
|
""" make process DPI aware, customtkinter elements will get scaled automatically,
|
2022-05-05 21:25:37 +03:00
|
|
|
only gets activated when CTk object is created """
|
|
|
|
|
2022-05-22 01:02:45 +03:00
|
|
|
if not cls.deactivate_automatic_dpi_awareness:
|
2022-05-05 21:25:37 +03:00
|
|
|
if sys.platform == "darwin":
|
|
|
|
pass # high DPI scaling works automatically on macOS
|
|
|
|
|
|
|
|
elif sys.platform.startswith("win"):
|
|
|
|
from ctypes import windll
|
|
|
|
windll.shcore.SetProcessDpiAwareness(2)
|
|
|
|
# Microsoft Docs: https://docs.microsoft.com/en-us/windows/win32/api/shellscalingapi/ne-shellscalingapi-process_dpi_awareness
|
|
|
|
else:
|
|
|
|
pass # DPI awareness on Linux not implemented
|
|
|
|
|
2022-04-19 01:01:33 +03:00
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def get_window_dpi_scaling(cls, window) -> float:
|
2022-05-26 20:16:01 +03:00
|
|
|
if not cls.deactivate_automatic_dpi_awareness:
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
return 1 # scaling works automatically on macOS
|
2022-04-19 01:01:33 +03:00
|
|
|
|
2022-05-26 20:16:01 +03:00
|
|
|
elif sys.platform.startswith("win"):
|
|
|
|
from ctypes import windll, pointer, wintypes
|
2022-04-19 01:01:33 +03:00
|
|
|
|
2022-05-26 20:16:01 +03:00
|
|
|
DPI100pc = 96 # DPI 96 is 100% scaling
|
|
|
|
DPI_type = 0 # MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2
|
|
|
|
window_hwnd = wintypes.HWND(window.winfo_id())
|
|
|
|
monitor_handle = windll.user32.MonitorFromWindow(window_hwnd, wintypes.DWORD(2)) # MONITOR_DEFAULTTONEAREST = 2
|
|
|
|
x_dpi, y_dpi = wintypes.UINT(), wintypes.UINT()
|
|
|
|
windll.shcore.GetDpiForMonitor(monitor_handle, DPI_type, pointer(x_dpi), pointer(y_dpi))
|
|
|
|
return (x_dpi.value + y_dpi.value) / (2 * DPI100pc)
|
2022-04-19 01:01:33 +03:00
|
|
|
|
2022-05-26 20:16:01 +03:00
|
|
|
else:
|
|
|
|
return 1 # DPI awareness on Linux not implemented
|
2022-04-19 01:01:33 +03:00
|
|
|
else:
|
2022-05-26 20:16:01 +03:00
|
|
|
return 1
|
2022-04-19 01:01:33 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def check_dpi_scaling(cls):
|
2022-05-16 17:51:19 +03:00
|
|
|
# check for every window if scaling value changed
|
2022-05-22 01:02:45 +03:00
|
|
|
for window in cls.window_widgets_dict:
|
2022-05-25 19:37:55 +03:00
|
|
|
if window.winfo_exists():
|
|
|
|
current_dpi_scaling_value = cls.get_window_dpi_scaling(window)
|
|
|
|
if current_dpi_scaling_value != cls.window_dpi_scaling_dict[window]:
|
|
|
|
cls.window_dpi_scaling_dict[window] = current_dpi_scaling_value
|
|
|
|
cls.update_scaling_callbacks_for_window(window)
|
2022-05-22 01:02:45 +03:00
|
|
|
|
2022-04-19 01:01:33 +03:00
|
|
|
# find an existing tkinter object for the next call of .after()
|
2022-05-22 21:26:31 +03:00
|
|
|
for app in cls.window_widgets_dict.keys():
|
2022-04-19 01:01:33 +03:00
|
|
|
try:
|
2022-05-22 21:26:31 +03:00
|
|
|
app.after(cls.update_loop_interval, cls.check_dpi_scaling)
|
2022-04-19 01:01:33 +03:00
|
|
|
return
|
|
|
|
except Exception:
|
|
|
|
continue
|
|
|
|
|
|
|
|
cls.update_loop_running = False
|