An alternative to the eval()
method suggested by @Ashok would be to access the image through a dictionary that has scores as keys and image instances as values. I assume the reason you don't pass the variable for the image instance directly is because score can change. If you know the scores beforehand, you can use a method similar to this:
images = {
'_1': tk.PhotoImage(file='1.gif'), # score is key, image instance is value
'_2': tk.PhotoImage(file='2.gif'),
'_3': tk.PhotoImage(file='3.gif')
}
score = 1
score = str(score)
user_score = '_' + score
print(user_score)
# set the image to the corresponding dict key
canvas.create_image((200,100), image=images[user_score])
You can also implement error handling (if score
is 4
) to handle KeyError
s, and display a default image or none at all.