Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff9d666

Browse files
authored
Merge pull request #25184 from meeseeksmachine/auto-backport-of-pr-25174-on-v3.7.x
Backport PR #25174 on branch v3.7.x (Accept LA icons for the toolbar)
2 parents 006d2f5 + 322a3e2 commit ff9d666

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

lib/matplotlib/backends/_backend_tk.py

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

lib/matplotlib/backends/backend_wx.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,9 @@ def _icon(name):
11121112
*name*, including the extension and relative to Matplotlib's "images"
11131113
data directory.
11141114
"""
1115-
image = np.array(PIL.Image.open(cbook._get_data_path("images", name)))
1115+
pilimg = PIL.Image.open(cbook._get_data_path("images", name))
1116+
# ensure RGBA as wx BitMap expects RGBA format
1117+
image = np.array(pilimg.convert("RGBA"))
11161118
try:
11171119
dark = wx.SystemSettings.GetAppearance().IsDark()
11181120
except AttributeError: # wxpython < 4.1

lib/matplotlib/tests/test_backend_tk.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
import pytest
99

10-
from matplotlib.testing import subprocess_run_helper
1110
from matplotlib import _c_internal_utils
11+
from matplotlib.testing import subprocess_run_helper
12+
1213

1314
_test_timeout = 60 # A reasonably safe value for slower architectures.
1415

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
import signal
88
import subprocess
99
import sys
10+
import tempfile
1011
import time
1112
import urllib.request
1213

14+
from PIL import Image
15+
1316
import pytest
1417

1518
import matplotlib as mpl
1619
from matplotlib import _c_internal_utils
20+
from matplotlib.backend_tools import ToolToggleBase
1721
from matplotlib.testing import subprocess_run_helper as _run_helper
1822

1923

@@ -71,6 +75,24 @@ def _get_testable_interactive_backends():
7175
_test_timeout = 60 # A reasonably safe value for slower architectures.
7276

7377

78+
def _test_toolbar_button_la_mode_icon(fig):
79+
# test a toolbar button icon using an image in LA mode (GH issue 25174)
80+
# create an icon in LA mode
81+
with tempfile.TemporaryDirectory() as tempdir:
82+
img = Image.new("LA", (26, 26))
83+
tmp_img_path = os.path.join(tempdir, "test_la_icon.png")
84+
img.save(tmp_img_path)
85+
86+
class CustomTool(ToolToggleBase):
87+
image = tmp_img_path
88+
description = "" # gtk3 backend does not allow None
89+
90+
toolmanager = fig.canvas.manager.toolmanager
91+
toolbar = fig.canvas.manager.toolbar
92+
toolmanager.add_tool("test", CustomTool)
93+
toolbar.add_tool("test", "group")
94+
95+
7496
# The source of this function gets extracted and run in another process, so it
7597
# must be fully self-contained.
7698
# Using a timer not only allows testing of timers (on other backends), but is
@@ -122,14 +144,17 @@ def check_alt_backend(alt_backend):
122144
if importlib.util.find_spec("cairocffi"):
123145
check_alt_backend(backend[:-3] + "cairo")
124146
check_alt_backend("svg")
125-
126147
mpl.use(backend, force=True)
127148

128149
fig, ax = plt.subplots()
129150
assert_equal(
130151
type(fig.canvas).__module__,
131152
"matplotlib.backends.backend_{}".format(backend))
132153

154+
if mpl.rcParams["toolbar"] == "toolmanager":
155+
# test toolbar button icon LA mode see GH issue 25174
156+
_test_toolbar_button_la_mode_icon(fig)
157+
133158
ax.plot([0, 1], [2, 3])
134159
if fig.canvas.toolbar: # i.e toolbar2.
135160
fig.canvas.toolbar.draw_rubberband(None, 1., 1, 2., 2)
@@ -168,11 +193,17 @@ def test_interactive_backend(env, toolbar):
168193
pytest.skip("toolmanager is not implemented for macosx.")
169194
if env["MPLBACKEND"] == "wx":
170195
pytest.skip("wx backend is deprecated; tests failed on appveyor")
171-
proc = _run_helper(_test_interactive_impl,
172-
json.dumps({"toolbar": toolbar}),
173-
timeout=_test_timeout,
174-
extra_env=env)
175-
196+
try:
197+
proc = _run_helper(
198+
_test_interactive_impl,
199+
json.dumps({"toolbar": toolbar}),
200+
timeout=_test_timeout,
201+
extra_env=env,
202+
)
203+
except subprocess.CalledProcessError as err:
204+
pytest.fail(
205+
"Subprocess failed to test intended behavior\n"
206+
+ str(err.stderr))
176207
assert proc.stdout.count("CloseEvent") == 1
177208

178209

0 commit comments

Comments
 (0)