ในการทำงานกับรูปภาพใน tkinter นั้น Python ได้จัดเตรียมชุดเครื่องมือ PIL หรือ Pillow มีฟังก์ชันในตัวมากมายที่สามารถใช้ควบคุมรูปภาพในรูปแบบต่างๆ ได้
ในการเปิดรูปภาพในวิดเจ็ตแคนวาส เราได้ใช้ create_image(x, y, image, **options) ตัวสร้าง เมื่อเราส่งค่า Image ไปที่ Constructor ค่านั้นจะแสดงรูปภาพในแคนวาส
ตัวอย่าง
# 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() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีรูปภาพในแคนวาส
