- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I update images on a Tkinter Canvas?
The PIL or Pillow package in Python provides a way to process the images in a program. We can open an image, manipulate the image for different use, and can use it to visualize the data as well. To display an image in an application, we generally refer to the Canvas widget. As canvas widget provides many functionalities to add images and objects in an application, we can use it to display the images.
To change a particular image, we can configure the canvas by using the itemconfig() constructor. It takes image files that need to be updated and displays them on the window.
For this example, use three images of your choice and save them in the same project directory.
# Import the required library from tkinter import * from tkinter import ttk from PIL import Image, ImageTk # Create an instance of tkinter frame win=Tk() # Set the geometry win.geometry("750x400") # Define function to update the image def update_image(): canvas.itemconfig(image_container,image=img2) # Create a canvas and add the image into it canvas=Canvas(win, width=650, height=350) canvas.pack() # Create a button to update the canvas image button=ttk.Button(win, text="Update", command=lambda:update_image()) button.pack() # Open an Image in a Variable img1=PhotoImage(file="logo.png") img2=PhotoImage(file="logo2.png") img3=PhotoImage(file="logo3.png") # Add image to the canvas image_container=canvas.create_image(0,0, anchor="nw",image=img1) win.mainloop()
Running the above code will display a window with a canvas and a button to update the canvas image.
Now, click the "Update" button to change the image.
- How do you create a Button on a Tkinter Canvas?
- How to update an image in a Tkinter Canvas?
- How do I get the background color of a Tkinter Canvas widget?
- How do I find out the size of a canvas item in Tkinter?
- How do I position the buttons on a Tkinter window?
- How to create a Button on a Tkinter Canvas?
- How to draw a line on a Tkinter canvas?
- How to draw an arc on a tkinter canvas?
- How to put an outline on a canvas text on Tkinter?
- How to draw a dashed line on a Tkinter canvas?
- Update HTML5 canvas rectangle on hover
- Tkinter - How to put an outline on a canvas text
- How do I close a tkinter window?
- How to draw a png image on a Python tkinter canvas?
- How do I make a transparent canvas in HTML5?