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

Skip to content

Accept LA icons for the toolbar #25174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ def _recolor_icon(image, color):
# 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:
# assure a RGBA image as foreground color is RGB
im = im.convert("RGBA")
image = ImageTk.PhotoImage(im.resize((size, size)), master=self)
button._ntimage = image

Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,9 @@ def _icon(name):
*name*, including the extension and relative to Matplotlib's "images"
data directory.
"""
image = np.array(PIL.Image.open(cbook._get_data_path("images", name)))
pilimg = PIL.Image.open(cbook._get_data_path("images", name))
# ensure RGBA as wx BitMap expects RGBA format
image = np.array(pilimg.convert("RGBA"))
try:
dark = wx.SystemSettings.GetAppearance().IsDark()
except AttributeError: # wxpython < 4.1
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/tests/test_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import pytest

from matplotlib.testing import subprocess_run_helper
from matplotlib import _c_internal_utils
from matplotlib.testing import subprocess_run_helper


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

Expand Down
43 changes: 37 additions & 6 deletions lib/matplotlib/tests/test_backends_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import signal
import subprocess
import sys
import tempfile
import time
import urllib.request

from PIL import Image

import pytest

import matplotlib as mpl
from matplotlib import _c_internal_utils
from matplotlib.backend_tools import ToolToggleBase
from matplotlib.testing import subprocess_run_helper as _run_helper


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


def _test_toolbar_button_la_mode_icon(fig):
# test a toolbar button icon using an image in LA mode (GH issue 25174)
# create an icon in LA mode
with tempfile.TemporaryDirectory() as tempdir:
img = Image.new("LA", (26, 26))
tmp_img_path = os.path.join(tempdir, "test_la_icon.png")
img.save(tmp_img_path)

class CustomTool(ToolToggleBase):
image = tmp_img_path
description = "" # gtk3 backend does not allow None

toolmanager = fig.canvas.manager.toolmanager
toolbar = fig.canvas.manager.toolbar
toolmanager.add_tool("test", CustomTool)
toolbar.add_tool("test", "group")


# The source of this function gets extracted and run in another process, so it
# must be fully self-contained.
# Using a timer not only allows testing of timers (on other backends), but is
Expand Down Expand Up @@ -122,14 +144,17 @@ def check_alt_backend(alt_backend):
if importlib.util.find_spec("cairocffi"):
check_alt_backend(backend[:-3] + "cairo")
check_alt_backend("svg")

mpl.use(backend, force=True)

fig, ax = plt.subplots()
assert_equal(
type(fig.canvas).__module__,
f"matplotlib.backends.backend_{backend}")

if mpl.rcParams["toolbar"] == "toolmanager":
# test toolbar button icon LA mode see GH issue 25174
_test_toolbar_button_la_mode_icon(fig)

ax.plot([0, 1], [2, 3])
if fig.canvas.toolbar: # i.e toolbar2.
fig.canvas.toolbar.draw_rubberband(None, 1., 1, 2., 2)
Expand Down Expand Up @@ -168,11 +193,17 @@ def test_interactive_backend(env, toolbar):
pytest.skip("toolmanager is not implemented for macosx.")
if env["MPLBACKEND"] == "wx":
pytest.skip("wx backend is deprecated; tests failed on appveyor")
proc = _run_helper(_test_interactive_impl,
json.dumps({"toolbar": toolbar}),
timeout=_test_timeout,
extra_env=env)

try:
proc = _run_helper(
_test_interactive_impl,
json.dumps({"toolbar": toolbar}),
timeout=_test_timeout,
extra_env=env,
)
except subprocess.CalledProcessError as err:
pytest.fail(
"Subprocess failed to test intended behavior\n"
+ str(err.stderr))
assert proc.stdout.count("CloseEvent") == 1


Expand Down