pythonfor-looptkintertkinter-canvasphotoimage

Can I assign a variable to an image made by a for loop in the loop?


I am creating a chinese checkers AI project, and I created the checkers using a for loop. What I want to ask is, is it possible to assign a variable to each checker I created with create_image with the for loop? I use this code:

black = []
black = PhotoImage(file="black.gif")
black_sub = black.subsample(8, 8)
for i in range(4):
    black_id.append(i)

    canvas.create_image(425 + 24 * i,800 - 10 - 45 * i, anchor=S, 
    image=black_sub)

for i in range(4):

    black_id.append(i+4)

    canvas.create_image(425 - 24 * i,800 - 10 - 45 * i, anchor=S, 
    image=black_sub)

Can I possibly assign each list number to it's corresponding checker?


Solution

  • Hard to define your question based of the given information, but you don't have to fetch ID-s, as it will be the index of your checker list, so you easily can get any of the checkers indexing with their id as follows:

    black = PhotoImage(file="black.gif")
    black_sub = black.subsample(8, 8)
    checkers = []
    
    for i in range(4):
        checkers.append( 
                        canvas.create_image(425 + 24 * i,800 - 10 - 45 * i, anchor=S, 
                        image=black_sub) 
                        )
    
    for i in range(4):
        checkers.append( 
                        canvas.create_image(425 - 24 * i,800 - 10 - 45 * i, anchor=S, 
                        image=black_sub) 
                        )
    
    checkers[5].move(10,10) # get some of the checker based on its ID