Skip to content

Fix scaling in Tk on non-Windows systems #28588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ def __init__(self, figure=None, master=None):
self._tkcanvas_image_region = self._tkcanvas.create_image(
w//2, h//2, image=self._tkphoto)
self._tkcanvas.bind("<Configure>", self.resize)
if sys.platform == 'win32':
self._tkcanvas.bind("<Map>", self._update_device_pixel_ratio)
self._tkcanvas.bind("<Map>", self._update_device_pixel_ratio)
self._tkcanvas.bind("<Key>", self.key_press)
self._tkcanvas.bind("<Motion>", self.motion_notify_event)
self._tkcanvas.bind("<Enter>", self.enter_notify_event)
Expand Down Expand Up @@ -234,11 +233,15 @@ def filter_destroy(event):
self._rubberband_rect_white = None

def _update_device_pixel_ratio(self, event=None):
# Tk gives scaling with respect to 72 DPI, but Windows screens are
# scaled vs 96 dpi, and pixel ratio settings are given in whole
# percentages, so round to 2 digits.
ratio = round(self._tkcanvas.tk.call('tk', 'scaling') / (96 / 72), 2)
if self._set_device_pixel_ratio(ratio):
ratio = None
if sys.platform == 'win32':
# Tk gives scaling with respect to 72 DPI, but Windows screens are
# scaled vs 96 dpi, and pixel ratio settings are given in whole
# percentages, so round to 2 digits.
ratio = round(self._tkcanvas.tk.call('tk', 'scaling') / (96 / 72), 2)
elif sys.platform == "linux":
ratio = self._tkcanvas.winfo_fpixels('1i') / 96
if ratio is not None and self._set_device_pixel_ratio(ratio):
# The easiest way to resize the canvas is to resize the canvas
# widget itself, since we implement all the logic for resizing the
# canvas backing store on that event.
Expand Down
Loading