I have multiple type1 = Label(image=type_none)
and get a list ['Ground', 'Rock', 'Water']
and want to change it to
type1 = Label(image=type_Ground)
type2 = Label(image=type_Rock)
etc. The type_Rock
are already defined, and contain Data i want to Display.
So i am looking for a way to dynamically change the Variable. Something like this.
for i in range(0, len(type_list)):
type{i} = Label(image=type_{type_list[i]})
In the end it should update the Images of the Labels 1 through X. I am still a beginner and cant find a way to do this. I would post the full code, but it is large and messy. In case you need to know, the Image i have to define as self.type_ground = ImageTk.PhotoImage(Image.open("img/Ground.png").resize(self.type_size))
Thanks for your time reading through my Problems :)
Edit: Thanks for the Answers, i initially looked at eval(), and it worked fine, but i ran into some issues and because you all said it was bad practices, i now use dicts and lists containing the Variables.
The Image can be easily created like so: type_img = {"Scaning": ImageTk.PhotoImage(Image.open("img/Scaning.png").resize(type_size))}
,
but the Tk.Label
objects, i needed to handle different.
First i create the Label wtype0 = Label(master, image=type_img["None"])
, place it in Tkinter, then store it in a list type_tklist = [self.wtype0]
Then when i want to change the images, i can simply
for i in range(0, len(weak1)):
self.type_tklist[i].configure(image=self.type_img[weak1[i]])
self.type_tklist[i].image = self.type_img[weak1[i]]