Python Forum
How to redraw an existing image to a different image TkInter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to redraw an existing image to a different image TkInter
#5
import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, height=100, width=100)
image = tk.PhotoImage(file='some_image_file.png')
x = canvas.create_image(50, 50, image=image)
y = canvas.create_line(0, 0, 100, 100)
canvas.pack()
print(x, type(x), y, type(y))
root.mainloop()
Output:
1 <class 'int'> 2 <class 'int'>
canvas.create_* returns an int. This is a unique ID for the object that was just created. In my example x is 1 and y is 2. If I created another canvas object it would probably be 3. To Python, the id is just an int and it doesn't provide any methods for configuring the canvas object. To modify the object you use canvas methods and pass the ID of the object you want to modify. This program gets a dictionary of all the options/attributes you can configure for a canvas image object.
import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, height=100, width=100)
canvas.pack()

image= tk.PhotoImage(file='image.png')
x = canvas.create_image(50, 50, image=image)
config_items = canvas.itemconfigure(x)  # Call without option arguments to get dictionary of all options
for items in config_items.items():
    print(items)
root.mainloop()
Output:
('activeimage', ('activeimage', '', '', '', '')) ('anchor', ('anchor', '', '', 'center', 'center')) ('disabledimage', ('disabledimage', '', '', '', '')) ('image', ('image', '', '', '', 'pyimage1')) ('state', ('state', '', '', '', '')) ('tags', ('tags', '', '', '', ''))
image is one of the options you can configure. This code uses itemconfigure to change the image after the canvas image object is created.
import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, height=100, width=100)
canvas.pack()

one = tk.PhotoImage(file='one.png')
two = tk.PhotoImage(file='two.png')
x = canvas.create_image(50, 50, image=one) # Create image object using image one
canvas.itemconfigure(x, image=two)         # Change image from one to two.
root.mainloop()
Other interesting options/atributes are "disabledimage" and "state". This code creates an image object that has a disabledimage. It uses the state option to toggle between the normal and disabled images.
import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, height=100, width=100)
canvas.pack()

def toggle():
    if canvas.itemconfigure(x)['state'][4] == tk.DISABLED:
        canvas.itemconfigure(x, state=tk.NORMAL)
    else:
        canvas.itemconfigure(x, state=tk.DISABLED)
    canvas.after(1000, toggle)

x = canvas.create_image(50, 50, image=tk.PhotoImage(file='one.png'), disabledimage=tk.PhotoImage(file='two.png'))

toggle()

root.mainloop()
Reply


Messages In This Thread
RE: How to redraw an existing image to a different image TkInter - by deanhystad - Jul-08-2021, 04:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to visualize neuron weights with PIL but getting a white image. pointdexter16 0 280 Apr-13-2024, 02:48 PM
Last Post: pointdexter16
  Tkinter: An image and label are not appearing. emont 7 945 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  image conversion Skaperen 7 1,690 Sep-20-2023, 07:29 PM
Last Post: Skaperen
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 4,757 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] Load image and show in label ko_fal 8 3,240 Oct-25-2022, 09:20 AM
Last Post: ko_fal
  [Tkinter] Image in Frame in Tabbed Widget Columbo 4 2,267 Sep-28-2022, 08:04 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,632 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,722 Jul-22-2022, 10:26 AM
Last Post: menator01
  [PyQt] Cannot Display Image after Selecting Image bintangkecil 4 2,691 Jun-12-2022, 08:18 AM
Last Post: Axel_Erfurt
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 4,052 Jan-07-2022, 10:10 PM
Last Post: finndude

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020