I have the following program that does not work if I run:
import tkinter as tk
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image, ImageTk
##MATPLOTLIB STUFF --------------------------------------------
fig, ax = plt.subplots(figsize = (5,5))
x = np.arange(10)
y = np.random.randn(10)
ax.plot(x,y)
fig.savefig('image1.png', bboxtoinches = 'tight')
##PIL STUFF --------------------------------------------------
root = tk.Tk()
graph = ImageTk.PhotoImage(file="image1.png")
w1 = tk.Label(root, image=graph).pack(side='right')
explanation = "A matplotlib plot"
w2 = tk.Label(root, justify=tk.LEFT, padx=10,
text=explanation).pack(side='left')
root.mainloop()
but DOES work if I delete the matplotlib stuff and run
import tkinter as tk
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image, ImageTk
##PIL STUFF --------------------------------------------------
root = tk.Tk()
graph = ImageTk.PhotoImage(file="image1.png")
w1 = tk.Label(root, image=graph).pack(side='right')
explanation = "A matplotlib plot"
w2 = tk.Label(root, justify=tk.LEFT, padx=10,
text=explanation).pack(side='left')
root.mainloop()
As a matter of fact, the line that seems to cause the problem is
fig, ax = plt.subplots(figsize = (5,5))
If I do include this line then I get the following error:
Traceback (most recent call last):
File "proj_1_images.py", line 20, in <module>
w1 = tk.Label(root, image=graph).pack(side='right')
File "C:\Users\Luke Polson\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 2760, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\Luke Polson\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 2293, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage10" doesn't exist
Why would matplotlib be causing this error?
代码对我来说运行顺利,您的问题是pyimage10存储在内存中,但是找不到路径,重新启动内核并清除内存中所有已声明的变量将解决此问题。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.