When I try to display an image with tkinter
"~ .png " could not be recognized.
Since it became, a memorandum
Python 3.7.9 Tvl/Tk 8.6 Pillow==7.2.0
Check the version of tkinter
import tkinter
tkinter._test()
import tkinter
root = tkinter.Tk()
#Front omitted
canvas = tkinter.Canvas(root, width=800, height=600)
canvas.pack()
img = tkinter.PhotoImage(file="my_picture.png ")
canvas.create_image(400, 300, image=img)
#Rear omitted
Apparently, tkinter's PhotoImage only touches on a fairly limited extension.
Solved with pillow
import tkinter
from PIL import ImageTk, Image
root = tkinter.Tk()
#Front omitted
canvas = tkinter.Canvas(root, width=400, height=600, bg="skyblue")
canvas.pack()
image = Image.open("my_picture.png ")
photo = ImageTk.PhotoImage(image, master=root)
canvas.create_image(200, 300, image=photo)
#Rear omitted
Recommended Posts