So I'm remaking a browser game where you guess a Pokémon based on its cry from the games. I have the main part of the quiz game done, but I want to make a cheat sheet to go with it. The cheat sheet is just 15 rows of ten Pokémon(buttons with the images of the Pokémon on them), and clicking on them will play their cry. What I want to do is make a for loop, where as long as x > 152 (the # of original Pokémon + 1), the loop will create images of each Pokémon at specific places. My problem is that a single instance of a PhotoImage will be updated when a new image is applied to it, leaving the space before it blank.
def cheatsheet():
sheet = Toplevel()
sheet.title("Pokédex of Cries")
sheet.geometry("1000x500")
sheet.resizable(False, True)
#mon keeps track of the Pokémon's dex number
mon = 1
xpos = 20
ypos = 20
in_row = 0
#this loops 151 times
while mon < 152:
#this makes a new row every ten spaces
if in_row == 10:
ypos += 84
xpos = 20
in_row = 0
#this is where I encounter problems. monImage is a single instance of a
#PhotoImage, and when the loop reiterates, monImage leaves the button before it
#completely blank. I have tested it and making a monImage2 does leave two buttons
#with images, but I would have to make 151 separate monImages.
monImage = PhotoImage(file=str(mon) + ".png")
Button(sheet, image=monImage).place(x=xpos+101, y=ypos)
mon += 1
in_row += 1
xpos += 101
sheet.mainloop()
Is there any way I can dynamically update the name of monImage to something like monImage2 during the for loop, thus creating a separate instance and multiple images? This isn't me being lazy, either, I've had to make a dictionary for each Pokémon's cry wav, rename a bunch of files one by one etc. I'm willing to put in the work, but if this automated method is possible to do, I want to know it to improve my programming for the future.
I have checked around for another question like this. I found someone else with my exact same problem and looking for the exact same solution. Except they were making a card game, and they would've only needed to make 52 images for the cards instead of 151 images for all the Pokémon. Their question wasn't answered, as they did not explain the problem well enough(which is why this post is so concise, I don't want it to be confusing).
If my solution isn't feasible but there is another solution, please share that too. I'm willing to rewrite this code, its not too much to rewrite anyway.
Lastly, I apologize if this is code sickening to look at. I am very new, I started three days ago and this is my first project. Thanks in advance.