使用tkinter和Python 3.2在画布中插入.jpg

Mat*_*ert 2 python jpeg tkinter python-3.x

所以我想在画布上放一个.jpg,我在互联网上找到的就是使用PIL,但我使用的是Python 3.2,所以PIL不起作用.如何使用Python 3.2在画布中插入.jpg?

小智 7

只是为了拯救现在正在寻找这个的任何人从狩猎周围的点点滴滴(就像我刚刚做的那样)

正如Martijn Pieters所说使用Pillow而不是PIL,但代码看起来一样

from tkinter import Tk, Canvas
from PIL import ImageTk, Image

root = Tk()

#Create a canvas
canvas = Canvas(root, width=400, height=300)
canvas.pack()

# Load the image file
im = Image.open('test_image.jpg')
# Put the image into a canvas compatible class, and stick in an
# arbitrary variable to the garbage collector doesn't destroy it
canvas.image = ImageTk.PhotoImage(im)
# Add the image to the canvas, and set the anchor to the top left / north west corner
canvas.create_image(0, 0, image=canvas.image, anchor='nw')

root.mainloop()
Run Code Online (Sandbox Code Playgroud)