ในการทำงานกับรูปภาพใน tkinter Python มีเครื่องมือ PIL หรือ Pillow toolkit ซึ่งมีฟังก์ชันในตัวมากมายที่สามารถใช้ปฏิบัติกับรูปภาพรูปแบบต่างๆ ได้
เพื่อเปิดรูปภาพในวิดเจ็ต Canvas เราต้องใช้คอนสตรัคเตอร์ create_image(x, y, image, **options) เมื่อเราส่งค่ารูปภาพให้คอนสตรัคเตอร์ มันจะแสดงรูปภาพใน Canvas
ตัวอย่าง
# Import the required libraries
from tkinter import *
from PIL import Image, ImageTk
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x600")
# Create a canvas widget
canvas = Canvas(win, width=700, height=600)
canvas.pack()
# Load the image
img = ImageTk.PhotoImage(file="Monalisa.png")
# Add the image in the canvas
canvas.create_image(350, 400, image=img, anchor="center")
win.mainloop()
ผลลัพธ์
การรันโค้ดด้านบนจะแสดงหน้าต่างที่มีรูปภาพใน Canvas
