Skip to content

Commit 2166113

Browse files
committed
Fix issue #25164
1 parent baebe44 commit 2166113

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ def _recolor_icon(image, color):
747747
# Use the high-resolution (48x48 px) icon if it exists and is needed
748748
with Image.open(path_large if (size > 24 and path_large.exists())
749749
else path_regular) as im:
750+
# assure a RGBA image as foreground color is RGB
751+
im = im.convert("RGBA")
750752
image = ImageTk.PhotoImage(im.resize((size, size)), master=self)
751753
button._ntimage = image
752754

lib/matplotlib/tests/test_backend_tk.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,42 @@ def test_never_update():
177177
# checks them.
178178

179179

180+
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
181+
@_isolated_tk_test(success_count=1)
182+
def test_toolbar_button_la_mode_icon():
183+
# test that icon in LA mode can be used for buttons
184+
# see GH#25164
185+
import tempfile
186+
import warnings
187+
188+
from PIL import Image
189+
190+
import matplotlib
191+
import matplotlib.pyplot as plt
192+
from matplotlib.backend_tools import ToolToggleBase
193+
194+
# tweaking toolbar raises an UserWarning
195+
with warnings.catch_warnings():
196+
warnings.simplefilter("ignore", UserWarning)
197+
matplotlib.rcParams["toolbar"] = "toolmanager"
198+
199+
# create an icon in LA mode
200+
with tempfile.TemporaryDirectory() as tempdir:
201+
img = Image.new("LA", (26, 26))
202+
tmp_img_path = os.path.join(tempdir, "test_la_icon.png")
203+
img.save(tmp_img_path)
204+
205+
class CustomTool(ToolToggleBase):
206+
image = tmp_img_path
207+
208+
fig = plt.figure()
209+
toolmanager = fig.canvas.manager.toolmanager
210+
toolbar = fig.canvas.manager.toolbar
211+
toolmanager.add_tool("test", CustomTool)
212+
toolbar.add_tool("test", "group")
213+
print("success")
214+
215+
180216
@_isolated_tk_test(success_count=2)
181217
def test_missing_back_button():
182218
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)