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
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: