Skip to content

Commit

Permalink
Merge pull request matplotlib#28588 from rnhmjoj/main
Browse files Browse the repository at this point in the history
Fix scaling in Tk on non-Windows systems
  • Loading branch information
QuLogic authored Sep 21, 2024
2 parents 5f962a3 + f95e8ee commit 8552c7a
Showing 1 changed file with 10 additions and 7 deletions.
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

0 comments on commit 8552c7a

Please sign in to comment.