如何更改Matplotlib图形窗口的图标?我的应用程序有一个按钮,打开带有Matplotlib绘制的图形的Figure窗口。我成功地修改了应用程序的图标,但Figure窗口仍然具有Tkinter的典型“Tk”图标。
如何更改Matplotlib图形窗口的图标?我的应用程序有一个按钮,打开带有Matplotlib绘制的图形的Figure窗口。我成功地修改了应用程序的图标,但Figure窗口仍然具有Tkinter的典型“Tk”图标。
imshow()
和 show()
创建图像的按钮之前,我以这种方式初始化该图像:plt.Figure()
thismanager = get_current_fig_manager()
thismanager.window.wm_iconbitmap("icon.ico")
show()
时,窗口将显示我想要的图标。对我来说,之前的答案并没有起作用,而需要以下方法:
from Tkinter import PhotoImage
import matplotlib
thismanager = matplotlib.pyplot.get_current_fig_manager()
img = PhotoImage(file='filename.ppm')
thismanager.window.tk.call('wm', 'iconphoto', thismanager.window._w, img)
现在Qt5Agg后端已经成为主流,我在这里补充一下。它与Sijie Chen的答案中概述的Qt4Agg后端非常相似(几乎相同)。
import os
import matplotlib.pyplot as plt
from PyQt5 import QtGui
# Whatever string that leads to the directory of the icon including its name
PATH_TO_ICON = os.path.dirname(__file__) + '/static/icons/icon.ico'
plt.get_current_fig_manager().window.setWindowIcon(QtGui.QIcon(PATH_TO_ICON))
我发现在使用PyQT5的OS X下,执行plt.get_current_fig_manager().window.setWindowIcon()
没有任何效果。要改变Dock图标,您必须在QApplication
实例上调用setWindowIcon()
,而不是在窗口上。
对我有效的方法是:
QApplication.instance().setWindowIcon(QtGui.QIcon(icon_path))
QApplication.instance()
在您实际创建图形之前将为None
,因此请先执行该操作。thismanager = plt.get_current_fig_manager()
from PyQt4 import QtGui
thismanager.window.setWindowIcon(QtGui.QIcon((os.path.join('res','shepherd.png'))))