【发布时间】:2021-12-18 08:12:21
【问题描述】:
以前在 Stack Overflow 上也有人问过类似的问题,但是没有一个涉及 GTK 后端,也没有一个涉及需要更改后端搜索的路径。这里的类似问题包括this 和this 等等。 GTK后端的相关代码为here。
顺便说一句,如果它是相关的,我正在使用 PyInstaller 冻结这个 Python 脚本,我想“更改”图标的原因是因为在某些 Windows 计算机上,我发现这些图标是断开的链接尽管图像出现在正确的位置,但导航工具栏:
\dist\<script name>\matplotlib\mpl-data\images\<image_file>-symbolic.svg
以及具有 GTK 后端 (f'{image_file}-symbolic.svg') 预期格式的图像。我不确定为什么会发生这个问题并且花了很多时间无处可去,所以我想一个可能的解决方法是将图像作为我的报告的一部分存储在我选择的位置并强制它们被使用脚本(以防它是一些奇怪的环境变量或路径问题)。使用下面的示例,我可以成功更改工具提示和图像/图标,但我无法更改图像的位置。 toolitems 的第三个参数控制图像:
list of toolitems to add to the toolbar, format is:
(
text, # the text of the button (often not visible to users)
tooltip_text, # the tooltip shown on hover (where possible)
image_file, # name of the image for the button (without the extension)
name_of_method, # name of the method in NavigationToolbar2 to call
)
显然,图像必须在预期的路径中,并且必须是不带扩展名的名称。后端代码指定:
image = Gtk.Image.new_from_gicon(
Gio.Icon.new_for_string(
str(cbook._get_data_path('images',
f'{image_file}-symbolic.svg'))),
Gtk.IconSize.LARGE_TOOLBAR)
所以我可以拍摄一个 svg 图像并将其放在我上面给出的路径中,最后附加 -symbolic 并让它工作。但是,有没有办法在与馈送到后端的默认目录不同的路径中执行此操作?我目前使用的代码如下:
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3
class MyToolbar(NavigationToolbar2GTK3):
def __init__(self, figure_canvas, window):
self.toolitems = (
('Home', 'Reset original view', 'home', 'home'),
('Back', 'Back to previous view', 'back', 'back'),
('Forward', 'Forward to next view', 'forward', 'forward'),
(None, None, None, None),
('Pan', 'Pan axes with left mouse, zoom with right', 'move', 'pan'),
('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'),
('Subplots', 'Configure subplots', 'subplots', 'configure_subplots'),
(None, None, None, None),
('Save', 'Save the figure', 'filesave', 'save_figure'),
)
NavigationToolbar2GTK3.__init__(self, figure_canvas, window)
self.navigation_toolbar = MyToolbar(self._gtkFigure, main)
self.navigation_toolbar.update()
我的期望是某些计算机上出现的断开链接是cbook._get_data_path(由后端使用)由于某种原因出现不同的结果,所以如果我可以在脚本中以某种方式更改它,也许我可以修补问题?任何帮助或指点将不胜感激!
编辑:在挖掘了更多之后,cbook 中的_get_data_path 看起来只是对matplotlib.get_data_path() 的调用。从 here 开始,在 matplotlib 中无法使用自定义数据路径(我一直在寻找 .set_data_path() 方法)。其中一位贡献者建议“获取本机 tk 小部件,并对 iconphoto() 和他们的朋友执行正确的调用。”由于我没有使用 Tkinter 后端,我仍然不确定如何继续。我相当肯定我上面概述的方法不起作用,需要另一种方法,类似于here。
【问题讨论】:
标签: python matplotlib gtk pyinstaller