Skip to content

[Bug]: png icon image fails to load for manually defined tool buttons #22131

Description

@bobeldering

Bug summary

The ToolBase class has the interface of overriding the property 'image' containing the path to an image file to be displayed as the icon of the tool button:
https://matplotlib.org/stable/api/backend_tools_api.html#matplotlib.backend_tools.ToolBase.image
This image fails to load on Qt5 backends for PNG image files.

Code for reproduction

import matplotlib
matplotlib.use("Qt5Agg")
matplotlib.rcParams["toolbar"] = "toolmanager"
from matplotlib.backend_tools import ToolToggleBase
import matplotlib.pyplot as plt

class ExampleToolToggle(ToolToggleBase):
    image = "/tmp/example.png"

figure, axis = plt.subplots()
tool_manager = figure.canvas.manager.toolmanager
tool_manager.add_tool("example", ExampleToolToggle)
toolbar = figure.canvas.manager.toolbar
toolbar.add_tool(tool_manager.get_tool("example"), "zoompan", 0)
plt.show()

Actual outcome

The name of the tool is displayed instead of the image in the toolbar.

Expected outcome

The image displayed in the toolbar.

Additional information

This bug was introduced in version 3.3.0 (3.2.2 works fine).
The problem is caused by ToolbarQt calling the _icon method of NavigationToolbar2QT at:

button.setIcon(NavigationToolbar2QT._icon(self, image_file))

The _icon method unconditionally changes the path by inserting '_large' before the '.png' suffix:

name = name.replace('.png', '_large.png')

Before version 3.3.0 a _icon method of ToolbarQt itself was called, which didn't have the path mangling.

Operating system

Ubuntu

Matplotlib Version

3.5.1

Matplotlib Backend

Qt5Agg

Python version

Python 3.8.12

Jupyter version

No response

Installation

pip

Activity

  1. oscargus commented on Jan 6, 2022

    @oscargus
    Member

    Should be possible to use isNull to check if the QPixMap exists and if not use the original file name.

    https://doc.qt.io/qtforpython-5/PySide2/QtGui/QPixmap.html#PySide2.QtGui.PySide2.QtGui.QPixmap.isNull

  2. timhoffm commented on Jan 6, 2022

    @timhoffm
    Member

    Thanks for the hint! It's even easier to check the file existence directly in python.

  3. oscargus commented on Jan 7, 2022

    @oscargus
    Member

    Yeah, your are correct. Your fix is clearly better than using isNull (except for the case that *_large.png exists, but is not a valid png, but nothing I had in mind when suggesting it.)

  4. added this to the v3.5.2 milestone on Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions