fixed some bugs on Windows

This commit is contained in:
TomSchimansky 2022-11-12 20:59:10 +01:00
parent 2f27611f3e
commit d306a9d010
5 changed files with 69 additions and 33 deletions

View File

@ -86,10 +86,8 @@ class CTk(tkinter.Tk, CTkAppearanceModeBaseClass, CTkScalingBaseClass):
def _update_dimensions_event(self, event=None): def _update_dimensions_event(self, event=None):
if not self._block_update_dimensions_event: if not self._block_update_dimensions_event:
# removed this because of python stackoverflow error with many label widgets detected_width = super().winfo_width() # detect current window size
# self.update_idletasks() detected_height = super().winfo_height()
detected_width = self.winfo_width() # detect current window size
detected_height = self.winfo_height()
# detected_width = event.width # detected_width = event.width
# detected_height = event.height # detected_height = event.height
@ -101,22 +99,20 @@ class CTk(tkinter.Tk, CTkAppearanceModeBaseClass, CTkScalingBaseClass):
def _set_scaling(self, new_widget_scaling, new_window_scaling): def _set_scaling(self, new_widget_scaling, new_window_scaling):
super()._set_scaling(new_widget_scaling, new_window_scaling) super()._set_scaling(new_widget_scaling, new_window_scaling)
# block update_dimensions_event to prevent current_width and current_height to get updated # Force new dimensions on window by using min, max, and geometry. Without min, max it won't work.
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().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().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{self._apply_window_scaling(self._current_height)}") super().geometry(f"{self._apply_window_scaling(self._current_width)}x{self._apply_window_scaling(self._current_height)}")
# set new scaled min and max with 400ms delay (otherwise it won't work for some reason) # set new scaled min and max with delay (delay prevents weird bug where window dimensions snap to unscaled dimensions when mouse releases window)
self.after(400, self._set_scaled_min_max) self.after(1000, self._set_scaled_min_max) # Why 1000ms delay? Experience! (Everything tested on Windows 11)
# release the blocking of update_dimensions_event after a small amount of time (slight delay is necessary) def block_update_dimensions_event(self):
def set_block_update_dimensions_event_false(): self._block_update_dimensions_event = False
def unblock_update_dimensions_event(self):
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): def _set_scaled_min_max(self):
if self._min_width is not None or self._min_height is not None: if self._min_width is not None or self._min_height is not None:
@ -157,12 +153,14 @@ class CTk(tkinter.Tk, CTkAppearanceModeBaseClass, CTkScalingBaseClass):
super().mainloop(*args, **kwargs) super().mainloop(*args, **kwargs)
def resizable(self, width: bool = None, height: bool = None): def resizable(self, width: bool = None, height: bool = None):
super().resizable(width, height) current_resizable_values = super().resizable(width, height)
self._last_resizable_args = ([], {"width": width, "height": height}) self._last_resizable_args = ([], {"width": width, "height": height})
if sys.platform.startswith("win"): if sys.platform.startswith("win"):
self._windows_set_titlebar_color(self._get_appearance_mode()) self._windows_set_titlebar_color(self._get_appearance_mode())
return current_resizable_values
def minsize(self, width: int = None, height: int = None): def minsize(self, width: int = None, height: int = None):
self._min_width = width self._min_width = width
self._min_height = height self._min_height = height

View File

@ -65,6 +65,8 @@ class CTkToplevel(tkinter.Toplevel, CTkAppearanceModeBaseClass, CTkScalingBaseCl
self.bind('<Configure>', self._update_dimensions_event) self.bind('<Configure>', self._update_dimensions_event)
self.bind('<FocusIn>', self._focus_in_event) self.bind('<FocusIn>', self._focus_in_event)
self._block_update_dimensions_event = False
def destroy(self): def destroy(self):
self._disable_macos_dark_title_bar() self._disable_macos_dark_title_bar()
@ -79,6 +81,7 @@ class CTkToplevel(tkinter.Toplevel, CTkAppearanceModeBaseClass, CTkScalingBaseCl
self.lift() self.lift()
def _update_dimensions_event(self, event=None): def _update_dimensions_event(self, event=None):
if not self._block_update_dimensions_event:
detected_width = self.winfo_width() # detect current window size detected_width = self.winfo_width() # detect current window size
detected_height = self.winfo_height() detected_height = self.winfo_height()
@ -89,14 +92,20 @@ class CTkToplevel(tkinter.Toplevel, CTkAppearanceModeBaseClass, CTkScalingBaseCl
def _set_scaling(self, new_widget_scaling, new_window_scaling): def _set_scaling(self, new_widget_scaling, new_window_scaling):
super()._set_scaling(new_widget_scaling, new_window_scaling) super()._set_scaling(new_widget_scaling, new_window_scaling)
# force new dimensions on window by using min, max, and geometry # Force new dimensions on window by using min, max, and geometry. Without min, max it won't work.
super().minsize(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().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) super().geometry(f"{self._apply_window_scaling(self._current_width)}x{self._apply_window_scaling(self._current_height)}")
self.after(400, self._set_scaled_min_max)
# set new scaled min and max with delay (delay prevents weird bug where window dimensions snap to unscaled dimensions when mouse releases window)
self.after(1000, self._set_scaled_min_max) # Why 1000ms delay? Experience! (Everything tested on Windows 11)
def block_update_dimensions_event(self):
self._block_update_dimensions_event = False
def unblock_update_dimensions_event(self):
self._block_update_dimensions_event = False
def _set_scaled_min_max(self): def _set_scaled_min_max(self):
if self._min_width is not None or self._min_height is not None: if self._min_width is not None or self._min_height is not None:
@ -127,12 +136,14 @@ class CTkToplevel(tkinter.Toplevel, CTkAppearanceModeBaseClass, CTkScalingBaseCl
super().iconify() super().iconify()
def resizable(self, width: bool = None, height: bool = None): def resizable(self, width: bool = None, height: bool = None):
super().resizable(width, height) current_resizable_values = super().resizable(width, height)
self._last_resizable_args = ([], {"width": width, "height": height}) self._last_resizable_args = ([], {"width": width, "height": height})
if sys.platform.startswith("win"): if sys.platform.startswith("win"):
self.after(10, lambda: self._windows_set_titlebar_color(self._get_appearance_mode())) self.after(10, lambda: self._windows_set_titlebar_color(self._get_appearance_mode()))
return current_resizable_values
def minsize(self, width=None, height=None): def minsize(self, width=None, height=None):
self._min_width = width self._min_width = width
self._min_height = height self._min_height = height

View File

@ -1,4 +1,3 @@
import ctypes.wintypes
import tkinter import tkinter
import sys import sys
from typing import Callable from typing import Callable
@ -14,8 +13,8 @@ class ScalingTracker:
window_scaling = 1 window_scaling = 1
update_loop_running = False update_loop_running = False
update_loop_interval = 600 # ms update_loop_interval = 100 # ms
loop_pause_after_new_scaling = 1000 # ms loop_pause_after_new_scaling = 1500 # ms
@classmethod @classmethod
def get_widget_scaling(cls, widget) -> float: def get_widget_scaling(cls, widget) -> float:
@ -119,9 +118,32 @@ class ScalingTracker:
pass # high DPI scaling works automatically on macOS pass # high DPI scaling works automatically on macOS
elif sys.platform.startswith("win"): elif sys.platform.startswith("win"):
from ctypes import windll, wintypes import ctypes
windll.shcore.SetProcessDpiAwareness(2)
# Microsoft Docs: https://docs.microsoft.com/en-us/windows/win32/api/shellscalingapi/ne-shellscalingapi-process_dpi_awareness # Values for SetProcessDpiAwareness and SetProcessDpiAwarenessContext:
# internal enum PROCESS_DPI_AWARENESS
# {
# Process_DPI_Unaware = 0,
# Process_System_DPI_Aware = 1,
# Process_Per_Monitor_DPI_Aware = 2
# }
#
# internal enum DPI_AWARENESS_CONTEXT
# {
# DPI_AWARENESS_CONTEXT_UNAWARE = 16,
# DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = 17,
# DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = 18,
# DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = 34
# }
# ctypes.windll.user32.SetProcessDpiAwarenessContext(34) # Non client area scaling at runtime (titlebar)
# does not work with resizable(False, False), window starts growing on monitor with different scaling (weird tkinter bug...)
# ctypes.windll.user32.EnableNonClientDpiScaling(hwnd) does not work for some reason (tested on Windows 11)
# It's too bad, that these Windows API methods don't work properly with tkinter. But I tested days with multiple monitor setups,
# and I don't think there is anything left to do. So this is the best option at the moment:
ctypes.windll.shcore.SetProcessDpiAwareness(2) # Titlebar does not scale at runtime
else: else:
pass # DPI awareness on Linux not implemented pass # DPI awareness on Linux not implemented
@ -161,10 +183,12 @@ class ScalingTracker:
if sys.platform.startswith("win"): if sys.platform.startswith("win"):
window.attributes("-alpha", 0.15) window.attributes("-alpha", 0.15)
window.block_update_dimensions_event()
cls.update_scaling_callbacks_for_window(window) cls.update_scaling_callbacks_for_window(window)
window.unblock_update_dimensions_event()
if sys.platform.startswith("win"): if sys.platform.startswith("win"):
window.after(200, lambda: window.attributes("-alpha", 1)) window.attributes("-alpha", 1)
new_scaling_detected = True new_scaling_detected = True

View File

@ -14,6 +14,7 @@ class App(customtkinter.CTk):
# configure window # configure window
self.title("CustomTkinter complex_example.py") self.title("CustomTkinter complex_example.py")
self.geometry(f"{1100}x{580}") self.geometry(f"{1100}x{580}")
#self.resizable(False, False)
# configure grid layout (4x4) # configure grid layout (4x4)
self.grid_columnconfigure(1, weight=1) self.grid_columnconfigure(1, weight=1)

View File

@ -8,6 +8,7 @@ class ToplevelWindow(customtkinter.CTkToplevel):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.protocol("WM_DELETE_WINDOW", self.closing) self.protocol("WM_DELETE_WINDOW", self.closing)
self.geometry("500x300") self.geometry("500x300")
self.resizable(False, False)
self.closing_event = closing_event self.closing_event = closing_event
self.label = customtkinter.CTkLabel(self, text="ToplevelWindow") self.label = customtkinter.CTkLabel(self, text="ToplevelWindow")
@ -26,6 +27,7 @@ class App(customtkinter.CTk):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.geometry("500x400") self.geometry("500x400")
self.resizable(False, False)
self.button_1 = customtkinter.CTkButton(self, text="Open CTkToplevel", command=self.open_toplevel) self.button_1 = customtkinter.CTkButton(self, text="Open CTkToplevel", command=self.open_toplevel)
self.button_1.pack(side="top", padx=40, pady=40) self.button_1.pack(side="top", padx=40, pady=40)