0

I just wanted to make variables with its own names with the eval() function(because tkinter doesn't want to use one image multiple times) but it seems not working.

iimages = 0
for line in range(0, len(maze)):
    mazeline = maze[line]
    for char in range(0, len(maze[line])):
        iimages += 1
        eval(f'image{iimages} = ""')
        eval(f'image{iimages} = tk.PhotoImage(file="Menus/Game Assets/wall1.png)")')
        eval(f'label = tk.Label(frame, image=image{iimages}).pack()')

Any ideas? It seems like it's a duplicate, but I didn't find any solutions in this topic.

Balla Botond
  • 55
  • 11
  • https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop possible duplicate – S P Sharan Jan 08 '22 at 13:36
  • You are creating the same image again and again? Whats the point? A single image can be used in n number of widgets anyway. Explain what you want again – Delrius Euphoria Jan 08 '22 at 13:48
  • 2
    Does this answer your question? [How do you create different variable names while in a loop?](https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop) – anymous Jan 08 '22 at 13:49
  • @CoolCloud I want to display a maze-like map in a grid, and I wanted to add image texture to the walls, but I've failed – Balla Botond Jan 08 '22 at 17:24
  • @BallaBotond I don't see any reason why the given code will not work, even a single image can be used on any widget. Is this inside a function? – Delrius Euphoria Jan 08 '22 at 17:40
  • @CoolCloud no, just I have a grid with ca. 50 different frames that need this one image. But somehow the program inserted the image only for the last appearance. But I've fixed it with the answer below – Balla Botond Jan 08 '22 at 18:01
  • @BallaBotond No, that is because only the last image is referenced, you need to keep the reference to all the images, if you can show a bit more code, you can avoid the bad practice. Or better, keep the image outside the loop. – Delrius Euphoria Jan 08 '22 at 18:43

1 Answers1

0

You can use exec() for this:

image = 7
exec(f'image_{image}="A string or something here"')

print(f"{image_7=}")
# image_7='A string or something here'

I don't know of a way to do it with eval() like you asked for.

user1717828
  • 7,122
  • 8
  • 34
  • 59
  • 2
    Shouldn't you be advising against the use of `exec`/`eval`? – Delrius Euphoria Jan 08 '22 at 14:14
  • @CoolCloud, if this were work, I would absolutely dig into the context where my coworker is doing this because I'm 99.9% sure there's a better way. But for an internet Q&A site there is no single context. Everyone coming from Google for the answer brings their own. So should we be advising people not use valid language features in a contextless situation? – user1717828 Jan 08 '22 at 14:25
  • Yea I think so, IMO, we gotta suggest this approach in the comment and clearly mention why there are better ways, but have to ask the OP to give more context. – Delrius Euphoria Jan 08 '22 at 14:38
  • Now I only have to fix some resolution problems but this will work! Thank you! – Balla Botond Jan 08 '22 at 17:27