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
#1
Hello. I have a tkinter application running and I am trying to come up with a solution. I will explain the scenario briefly:

On my tkinter application, there is a "toggle" button that toggles the sensors ON and OFF. I have uploaded a green "tick" and red "cross" images to display the current state of the sensors. When the sensor is ON, a green "tick" image must be displayed , and when the sensor is toggled OFF red "cross" is displayed.


See the image of the button and green tick:
https://ibb.co/2qBLPTf



When the program starts, the default state is all the sensors are turned on, hence I create an image with a green tick next to the button:
        img_tick1 = Image.open("/home/pi/Desktop/PICK_TO_LIGHT/green_tick.png")
        img_tick1 = img_tick1.resize((50,50),Image.ANTIALIAS)
        img_green_tick = ImageTk.PhotoImage(img_tick1)
        self.img_tick = canvas.create_image(430,120,anchor='nw',image=img_green_tick)
I have assigned a variable for the canvas.create_image and called it self.img_tick (self keyword is used because this is a class member but you can ignore it).

When the button is pressed, the following function is being executed as an event, at the end of the function, I want to redraw an existing image instead of green tick to a red cross:
    def toggle_sensor(self):
        for x in range(0,len(active_devices)):
            while True:
                try:
                    master_modbus.execute(active_devices[x], cst.WRITE_SINGLE_REGISTER, 10, output_value=66)
                    break
                except Exception as e:
                    print("exception on remove item:",e)
        # swap the image with red tick here!
        #self.tick_img.config(image=img_red_tick)
Notice I have tried to use .config but it did not work as the create_image does not have a config method attached to it. Please could someone suggest me a possible solution for this? How can I redraw an image? I know this may not be the most efficient solution but please suggest me a possible fixes for this particular example

UPDATE

I may have found a solution but its not complete yet:

        canvas.delete(self.img_tick)
        self.img_tick = canvas.create_image(430,115,anchor='nw',image=self.img_red_tick)
The code above will delete the previous image and draw a new one at the same coordinates. However, now the next issue is how do I know what was the previous image. I can create a global variable and keep toggling it so I know what is the current state, but that is not the best solution in my opinion.

According to the following information about the create.canvas_image:
https://anzeljg.github.io/rin2/book2/240...image.html

This constructor returns the integer ID number of the image object for that canvas.. Is it possible to determine whether the current image is set to green tick or red cross based on the constructor ID?
Reply


Messages In This Thread
How to redraw an existing image to a different image TkInter - by zazas321 - Jul-08-2021, 10:51 AM

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 194 Apr-13-2024, 02:48 PM
Last Post: pointdexter16
  Tkinter: An image and label are not appearing. emont 7 784 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  image conversion Skaperen 7 1,626 Sep-20-2023, 07:29 PM
Last Post: Skaperen
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 4,495 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] Load image and show in label ko_fal 8 3,151 Oct-25-2022, 09:20 AM
Last Post: ko_fal
  [Tkinter] Image in Frame in Tabbed Widget Columbo 4 2,199 Sep-28-2022, 08:04 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,541 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,678 Jul-22-2022, 10:26 AM
Last Post: menator01
  [PyQt] Cannot Display Image after Selecting Image bintangkecil 4 2,633 Jun-12-2022, 08:18 AM
Last Post: Axel_Erfurt
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 3,980 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