1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

tkinterで画像が表示できない時の解決【python】

Posted at

tkinterで画像を表示させようとしたら

"~.png"は認識できませんでした。

となったので備忘録

#環境
Python 3.7.9
Tvl/Tk 8.6
Pillow==7.2.0

tkinterのバージョン確認

import tkinter
tkinter._test() 

#本題

##問題のコード


import tkinter
root = tkinter.Tk()

#前部省略

canvas = tkinter.Canvas(root, width=800, height=600)
canvas.pack()

img = tkinter.PhotoImage(file="my_picture.png")
canvas.create_image(400, 300, image=img)


#後部省略

どうやらtkinterのPhotoImageはかなり限定的な拡張子しか触れない様子。

pillowを使って解決

##解決コード


import tkinter
from PIL import ImageTk, Image

root = tkinter.Tk()

#前部省略

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)


#後部省略

#以上

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?