Home > database >  How to refresh a image in tkinter
How to refresh a image in tkinter

Time:12-08

I'm new to Tkinter and I'm working on a noise terrain generator that users may edit. However, I'm having trouble with my picture on the canvas not updating when I push the button. My tkinter has two buttons: Generate (which generates the button and displays it on my canvas) and Refresh (which clears the image on my canvas). The problem is that if I generate another image with the same name file of the picture and display it to the canvas ( Generate ), it does not appear. What is the best method for overcoming these problem?

Here is my code :

self.Preview_Image = tk.Canvas(top, height=600, width=600)
            self.Preview_Image.pack()

            self.image = (Image.open("No_image.jpeg"))
            self.resize_image = self.image.resize((600, 450), Image.ANTIALIAS)
            self.img = ImageTk.PhotoImage(self.resize_image)

            self.update_img = ImageTk.PhotoImage(Image.open("result_veg.jpeg"))

            self.image_id = self.Preview_Image.create_image(-115, -50, image=self.img, anchor=tk.NW, )
            self.Preview_Image.place(relx=0.017, rely=0.067, relheight=0.784
                    , relwidth=0.605)

The self.image is the default image when i open the app, while self.update_img is when i Generate the button and display it on my canvas

Buttons :

            self.Generate_button = tk.Button(top, command=self.combineFunc(get_result,lambda:update_image(self)))
            self.Generate_button.place(relx=0.667, rely=0.867, height=24, width=77)
            self.Generate_button.configure(activebackground="#ececec",
                                    activeforeground="#000000",
                                    background="#d9d9d9",
                                    disabledforeground="#a3a3a3",
                                    foreground="#000000",
                                    highlightbackground="#d9d9d9",
                                    highlightcolor="black",
                                    pady="0",
                                    text='''Generate''',)

            self.Refresh_button = tk.Button(top, command=lambda:refresh(self))
            self.Refresh_button.place(relx=0.85, rely=0.867, height=24, width=67)
            self.Refresh_button.configure(activebackground="#ececec",
                                    activeforeground="#000000",
                                    background="#d9d9d9",
                                    disabledforeground="#a3a3a3",
                                    foreground="#000000",
                                    highlightbackground="#d9d9d9",
                                    highlightcolor="black",
                                    pady="0",
                                    text='''Refresh''',)

Commands of my Buttons :

def update_image(self, *args , **kwargs): 
                  self.Preview_Image.itemconfig(self.image_id, image = self.update_img)

def refresh(self, *args, **kwargs):
             self.Preview_Image.delete(self.image_id)
             print("refresh")

If you want a more detailed description of my code, here

CodePudding user response:

Try :

def update_image(self, *args , **kwargs): 
       self.Preview_Image.itemconfig(self.image_id, image = self.update_img)
       self.Preview_Image.image = self.update_img

Refer to this link

CodePudding user response:

After you have updated the image file result_veg.jpeg inside get_call(), you need to reload it and assign to self.update_img:

def update_image(self, *args, **kwargs):
    # reload the updated result_veg.jpeg
    self.update_img = ImageTk.PhotoImage(file='result_veg.jpeg')
    self.Preview_Image.itemconfig(self.image_id, image=self.update_img)
  • Related