Home > Back-end >  Generating variables in python?
Generating variables in python?

Time:01-09

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.

CodePudding user response:

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.

  •  Tags:  
  • Related