我想使用我加载的特定图像(.gif 图像)替换 matplotlib 和 tkinter 窗口中的默认图标。它在主窗口(plt.show() matplotlib 窗口)上工作,但是当我通过单击 matplotlib 窗口上的按钮打开 tkinter 窗口时,我无法更改 tkinter 图标,我收到错误“_tkinter.TclError:无法使用“pyimage19”作为图标照片:不是照片图像”。但是,当我不替换 matplotlib 图标时,tkinter 窗口的图标会发生变化(顺便说一句,我无法同时替换 2 个窗口的图标,这就是问题所在)。
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()
我尝试加载 2 张不同的图片,但出现了同样的错误。我也尝试使用这个命令“fenetre.tk.call("wm","iconphoto",fenetre._w,tk.PhotoImage(file="accueil.gif"))”,但还是出现了同样的错误。
谢谢你的帮助