Change icon in a Matplotlib figure window

前端 未结 5 1169
难免孤独
难免孤独 2020-12-11 05:18

Is it possible to change the icon of a Matplotlibe figure window? My application has a button that opens a Figure window with a graph (created with Matplotlib). I managed to

相关标签:
5条回答
  • 2020-12-11 05:34

    If you are using Qt4Agg backend, the following code may help you:

    thismanager = plt.get_current_fig_manager()
    from PyQt4 import QtGui
    thismanager.window.setWindowIcon(QtGui.QIcon((os.path.join('res','shepherd.png'))))
    
    0 讨论(0)
  • 2020-12-11 05:37

    I solved it in this way: BEFORE I press the button that creates the figure with imshow() and show(), I initialize the figure in this way:

    plt.Figure()
    thismanager = get_current_fig_manager()
    thismanager.window.wm_iconbitmap("icon.ico")
    

    so when I press show() the window has the icon I want.

    0 讨论(0)
  • 2020-12-11 05:37

    Just adding this here, now that the Qt5Agg backend has made it's way into the mainstream. It's similar (pretty much the same) to the Qt4Agg backend as outlined by Sijie Chen's answer.

    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))
    
    0 讨论(0)
  • 2020-12-11 05:51

    I found that under OS X with PyQT5, doing plt.get_current_fig_manager().window.setWindowIcon() has no effect. To get the dock icon to change you have to call setWindowIcon() on the QApplication instance, not on the window.

    What worked for me is:

    QApplication.instance().setWindowIcon(QtGui.QIcon(icon_path))
    

    Do mind that QApplication.instance() will be None until you have actually created a figure, so do that first.

    0 讨论(0)
  • 2020-12-11 05:58

    For me the previous answer did not work, rather the following was required:

    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)
    
    0 讨论(0)
提交回复
热议问题