Thanks SBKubic,
Optimized schemes (such as functions) no longer inherit the actual dict of local variables but instead receive a copy when calling locals(). Mutating that dict does not affect the actual local namespace; therefore, setting locals()[name] = value does not create the variable in the function, and subsequent calls to locals()[name] will still cause it to be missing, raising a KeyError when accessing it. This is part of the new semantics for locals() introduced in Python 3.13 (PEP 667).
In my example, since, as I mentioned, the three lines are in a loop, the Tkinter text widget only keeps visible the images that still have a live reference in Python. In my loop, I'm assigning each new PhotoImage to the same global variable locals_variable, so previous references are lost and removed by Python's garbage collector. That's why only the last inserted image remains.
To keep all PhotoImage instances alive, for example, store them in a global list or as a widget attribute:
imagenes = []
def add_image():
img = PhotoImage(file='../Figuras_Ayuda/Fig10.png')
imagenes.append(img)
my_text.image_create(END,image=img)