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
#4
I would use a label for this.
class Lamp(tk.Label):
    """Use images to display True/False value"""
    def __init__(self, window, on_img, off_img, **kvargs):
        super().__init__(window, image=off_img, **kvargs)
        self.images = (off_img, on_img)
        self._value = False

    @property
    def value(self):
        """Get current value"""
        return self._value

    @value.setter
    def value(self, value):
        """Set current value.  If value changed, update image to reflect new value"""
        if value != self._value:
            self._value = value
            self['image'] = self.images[int(self._value)]

def toggle():
    """Toggle value every second to demonstrate lamp"""
    lamp.value = not lamp.value
    lamp.after(1000, toggle)

root = tk.Tk()
lamp = Lamp(root, tk.PhotoImage(file=IMAGE_DIR/'ttt_x.png'), tk.PhotoImage(file=IMAGE_DIR/'ttt_o.png'))
lamp.pack()
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, 02:36 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 147 Apr-13-2024, 02:48 PM
Last Post: pointdexter16
  Tkinter: An image and label are not appearing. emont 7 680 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  image conversion Skaperen 7 1,573 Sep-20-2023, 07:29 PM
Last Post: Skaperen
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 4,383 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] Load image and show in label ko_fal 8 3,094 Oct-25-2022, 09:20 AM
Last Post: ko_fal
  [Tkinter] Image in Frame in Tabbed Widget Columbo 4 2,166 Sep-28-2022, 08:04 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,473 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,641 Jul-22-2022, 10:26 AM
Last Post: menator01
  [PyQt] Cannot Display Image after Selecting Image bintangkecil 4 2,605 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,937 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