Tkinter之canvas-image
https://blog.csdn.net/sailist/article/details/79459185
关于image有两个重要的点要注意,一个是格式,第二是要保持持续引用
1.This should be a PhotoImage or BitmapImage, or a compatible object (such as the PIL PhotoImage).
2.The application must keep a reference to the image object.
因此需要提前声明全局变量:
im1=None
im2=None
示例:
from Tkinter import *
from PIL import Image
from PIL import ImageTk
window=Tk()
window.title('My Window')
window.geometry('600x800')
# 在图形界面上创建 500 *500 大小的画布
canvas = Canvas(window, bg='green', height=500, width=500)
canvas.pack()
# 说明图片位置,并导入图片到画布上
im1=None
im2=None
im1 = Image.open("pic.gif") #支持相对或绝对路径,支持多种格式
im2 = ImageTk.PhotoImage(im1)
canvas.create_image(10,10,anchor=NW,image=im2)
window.mainloop()