- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 to update an image in a Tkinter Canvas?
Canvas can be used to play with images, animating objects, 3D modeling, displaying text, and many more. Moreover, we can display an image file using the create_image() constructor.
Following this, let us build an application that can update the canvas images locally. We can add a button to trigger the event which when pressed will change the canvas image.
To change a particular image, we can configure the canvas by using the itemconfig() constructor. It takes image files which need to be updated and displayed them on the window.
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 Image" button to update the canvas Image.
- How to insert an image in a Tkinter canvas item?
- How to center an image in canvas Python Tkinter
- How to Move an Image in Tkinter canvas with Arrow Keys?
- Embedding an Image in a Tkinter Canvas widget using PIL
- How do I update images on a Tkinter Canvas?
- How to open PIL Image in Tkinter on Canvas?
- How to draw a png image on a Python tkinter canvas?
- How to update the image of a Tkinter Label widget?
- How to draw an arc on a tkinter canvas?
- How to add an image in Tkinter?
- Tkinter - How to put an outline on a canvas text
- How to resize an image using Tkinter?
- How to use an Image as a button in Tkinter?
- How to place an image into a frame in Tkinter?
- How to clear Tkinter Canvas?