'my loop doesn't seem to reset when deleting the frame. (Tkinter)

I have an issue where I render the images in from a certain directory. this works fine and I have no issue with it until I destroy the frame and try to load the images in again.

it seems that the values don't reset and it tries to carry on from where it left off and I am really confused as to what's going on.

I have included the code to how I am loading them in also images which show what's happening.

sorry if my code is messy I am new to this

column_x = 0
row_y = 1
imageCount = 0
listOfImagesAndLabels = []


def show_all():
    global file
    global column_x
    global row_y
    global imageCount
    friendFrame = LabelFrame(root, background="grey")
    friendFrame.grid(row=1, sticky="NW")
    for i in range(amountOfPictures):
        imageCount += 1
        imagesOpen = Image.open(file)
        imageResize = imagesOpen.resize((125, 150), Image.NEAREST)
        imageFinalState = ImageTk.PhotoImage(imageResize)
        imageLabel = ttk.Label(friendFrame, compound="top", image=imageFinalState, text=file.split("/")[-1],
                               relief=RAISED)
        imageLabel.grid(row=row_y, column=column_x)
        imageLabel.imageFinalState = imageFinalState
        column_x += 1
        file = "images/" + dirList[imageCount]
        if column_x >= 3:
            column_x = 0
            row_y += 1
            
            
            
# grabs the full directory of the stored images
dirList = os.listdir("images/")
file = "images/" + dirList[0]
amountOfPictures = len(dirList)
print(amountOfPictures)

def clear_all():
    global friendFrame
    clearPrompt = tk.messagebox.askquestion("Clear All", "Are you sure you want to clear all friends?", icon="question")
    if clearPrompt == "yes":
        friendFrame.destroy()

enter image description here

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source