ins.gif的下载地址:https://morvanzhou.github.io/static/results/tkinter/ins.gif
笔者用jupyter notebook运行下面这段代码时,发现了一个诡异之处,有时能成功运行,有时会报错:
TclError: image "pyimage13" doesn't exist
同一段代码,为什么有时能成功运行,有时会报错,笔者想不明白,如果哪位大佬发现了问题,请给笔者留言。
# 创建主窗口
import tkinter as tk
window = tk.Tk()
window.title('画布')
window.geometry('300x200')
# 创建画布
canvas = tk.Canvas(window, bg='blue', width='200', height=100)
# 往画布中插入图片
image_file = tk.PhotoImage(file='ins.gif') # 引入图片AI.gif
image = canvas.create_image(10, 10, # 10, 10是图片放入画布中的坐标
anchor='nw', # nw是northwest的简写,把图片的左上角作为锚定点
image=image_file,
)
# 在画布中绘制直线
x0, y0, x1, y1 = 50, 50, 80, 80
line = canvas.