import tkinter as tk from PIL import ImageTk, Image root = tk.Tk() canvas = tk.Canvas(root, width=500, height=500) canvas.pack() image = Image.open("example.png") image = ImageTk.PhotoImage(image) canvas.create_image(250, 250, image=image) root.mainloop()
import tkinter as tk from PIL import ImageTk, Image def display_image(): image = Image.open("example2.png") image = ImageTk.PhotoImage(image) canvas.create_image(250, 250, image=image) root = tk.Tk() frame = tk.Frame(root) frame.pack() canvas = tk.Canvas(frame, width=500, height=500) canvas.pack() button = tk.Button(frame, text="Display Image", command=display_image) button.pack() root.mainloop()In this example, we are adding a button widget to the frame and setting its command to a function called "display_image". When the button is clicked, this function will load and display a new image ("example2.png") on the Canvas. The rest of the code is similar to the previous example. The package library used in these examples is Tkinter, which is built into Python and does not require any external installation.