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:
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:
UPDATE
I may have found a solution but its not complete yet:
According to the following information about the create.canvas_image:
https://anzeljg.github.io/rin2/book2/240...image.html
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?