I am trying to build an image viewer using Tkinter but I am stuck at this weird problem
Keep a reference to the image
from tkinter import *from PIL import ImageTk, Imageimport osdef moveForward(): global current_img global currImageIndex global imgLabel currImageIndex += 1 imgLabel.grid_forget() print(picturesList) img = ImageTk.PhotoImage(Image.open(picturesList[currImageIndex])) current_img = img imgLabel = Label(root,image=img) imgLabel.grid(row=0,column=1)picturesList = []#Here is the variable where the reference will be storedcurrent_img = Nonefor pictures in os.listdir('images'): if pictures.startswith('img'): picturesList.append('images/'+pictures)currImageIndex = 0root = Tk()img = ImageTk.PhotoImage(Image.open("images/img5.jpg"))imgLabel = Label(image=img)imgLabel.grid(row=0,column=1)backwardImg = ImageTk.PhotoImage(Image.open("images/backward.ico"))backButton = Button(image=backwardImg,width=80,height=80,relief=FLAT)backButton.grid(row=0,column=0)forwardImg = ImageTk.PhotoImage(Image.open("images/forward.ico"))forwardButton = Button(image=forwardImg, width=80, height=80, relief=FLAT, command=moveForward)forwardButton.grid(row=0,column=2)root.mainloop()