1

I am looking for replacing the default icone from the matplotlib and tkinter window by using a specific image I load (.gif image). It works on the main window (the plt.show() matplotlib window), but when I open a tkinter window by clicking on the button placed on the matplotlib window, I can't change the tkinter icone, I got the error "_tkinter.TclError: can't use "pyimage19" as iconphoto: not a photo image". However, when I don't replace the matplotlib icone, the icone of the tkinter window changes (by the way I can't replace the icone of the 2 windows at the same time, here is the problem).

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import tkinter as tk
from PIL import Image,ImageTk

def ouvrir_fenetre(event):
    global fenetre

    try:fenetre.destroy()
    except:pass

    fenetre = tk.Tk()
    fenetre.title("Fenêtre Tkinter")

##    fenetre.tk.call("wm","iconphoto",fenetre._w,tk.PhotoImage(file="accueil.gif"))
    image_tkinter = Image.open('accueil.gif')
    photo_tkinter = ImageTk.PhotoImage(image_tkinter) # Error: "_tkinter.TclError: can't use "pyimage19" as iconphoto: not a photo image"
    fenetre.iconphoto(False, photo_tkinter)

    fenetre.mainloop()

fig, ax = plt.subplots()

manager = plt.get_current_fig_manager()
window = manager.window
image_matplotlib = Image.open('accueil.gif')
photo_matplotlib = ImageTk.PhotoImage(image_matplotlib)
window.iconphoto(False, photo_matplotlib)

button = plt.axes([0.9, 0, 0.1, 0.05])
btn = plt.Button(button, 'Tkinter')
btn.on_clicked(ouvrir_fenetre)

plt.show()

I tried to load 2 different images, I got the same error. I tried too to use this command "fenetre.tk.call("wm","iconphoto",fenetre._w,tk.PhotoImage(file="accueil.gif"))", but I got the same error.

Thank you for the help

2
  • try with a .jpeg/.png - do you have the same issue ? Commented Mar 8 at 12:59
  • Yes, got the dame error Commented Mar 8 at 16:38

1 Answer 1

2

but when I open a tkinter window by clicking on the button placed on the matplotlib window, I can't change the tkinter icone, I got the error "_tkinter.TclError: can't use "pyimage19" as iconphoto: not a photo image".

"pyimage19" is not a photo image; it is an iconphoto. The reason for this could be that you are using tk.TK(), which does not open a new window.

The problem an be fixed.

Change this on line 13:

fenetre = tk.Tk()

to:

fenetre = tk.Toplevel()

Screenshot:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.