import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=400, height=400) canvas.pack() canvas.create_rectangle(50, 50, 200, 200, fill="blue") root.mainloop()
import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=400, height=400) canvas.pack() photo = tk.PhotoImage(file="image.gif") canvas.create_image(0, 0, anchor=tk.NW, image=photo) root.mainloop()In this example, an image is loaded into a canvas widget using the PhotoImage method and the create_image method is used to place the image on the canvas. The anchor parameter is set to NW (NorthWest) to specify the position of the image on the canvas.