From 9073feddb961e7b8c99d9e479a856e4193cd362f Mon Sep 17 00:00:00 2001 From: daniilS Date: Fri, 7 Jan 2022 20:39:34 +0000 Subject: [PATCH] Fix loading user-defined icons for Tk toolbar Co-Authored-By: Elliott Sales de Andrade --- lib/matplotlib/backends/_backend_tk.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index a5518ec11667..4bc148ad04f5 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -643,9 +643,15 @@ def _set_image_for_button(self, button): if button._image_file is None: return + # Allow _image_file to be relative to Matplotlib's "images" data + # directory. + path_regular = cbook._get_data_path('images', button._image_file) + path_large = path_regular.with_name( + path_regular.name.replace('.png', '_large.png')) size = button.winfo_pixels('18p') - with Image.open(button._image_file.replace('.png', '_large.png') - if size > 24 else button._image_file) as im: + # Use the high-resolution (48x48 px) icon if it exists and is needed + with Image.open(path_large if (size > 24 and path_large.exists()) + else path_regular) as im: image = ImageTk.PhotoImage(im.resize((size, size)), master=self) button.configure(image=image, height='18p', width='18p') button._ntimage = image # Prevent garbage collection.