@@ -166,7 +166,7 @@ class FigureCanvasTk(FigureCanvasBase):
166
166
def __init__ (self , figure , master = None , resize_callback = None ):
167
167
super ().__init__ (figure )
168
168
self ._idle_draw_id = None
169
- w , h = self .figure . bbox . size . astype ( int )
169
+ w , h = self .get_width_height ( physical = True )
170
170
self ._tkcanvas = tk .Canvas (
171
171
master = master , background = "white" ,
172
172
width = w , height = h , borderwidth = 0 , highlightthickness = 0 )
@@ -175,6 +175,7 @@ def __init__(self, figure, master=None, resize_callback=None):
175
175
self ._tkcanvas .create_image (w // 2 , h // 2 , image = self ._tkphoto )
176
176
self ._resize_callback = resize_callback
177
177
self ._tkcanvas .bind ("<Configure>" , self .resize )
178
+ self ._tkcanvas .bind ("<Map>" , self ._update_pixel_ratio )
178
179
self ._tkcanvas .bind ("<Key>" , self .key_press )
179
180
self ._tkcanvas .bind ("<Motion>" , self .motion_notify_event )
180
181
self ._tkcanvas .bind ("<Enter>" , self .enter_notify_event )
@@ -209,6 +210,20 @@ def filter_destroy(event):
209
210
self ._master = master
210
211
self ._tkcanvas .focus_set ()
211
212
213
+ def _update_pixel_ratio (self , event = None ):
214
+ # Tk gives scaling with respect to 72 DPI, but most (all?) screens are
215
+ # scaled vs 96 dpi, and pixel ratio settings are given in whole
216
+ # percentages, so round to 2 digits.
217
+ current_pixel_ratio = round (
218
+ self ._master .call ('tk' , 'scaling' ) / (96 / 72 ), 2 )
219
+ if self .pixel_ratio != current_pixel_ratio :
220
+ self .pixel_ratio = current_pixel_ratio
221
+ # The easiest way to resize the canvas is to resize the canvas
222
+ # widget itself, since we implement all the logic for resizing the
223
+ # canvas backing store on that event.
224
+ w , h = self .get_width_height (physical = True )
225
+ self ._tkcanvas .configure (width = w , height = h )
226
+
212
227
def resize (self , event ):
213
228
width , height = event .width , event .height
214
229
if self ._resize_callback is not None :
0 commit comments