0

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]]
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Beardlongo
  • 69
  • 5
  • 1
    Just create a list or dict of Labels. Creating dynamic variable names is not the way to go. – Paul Rooney Sep 23 '20 at 00:35
  • 1
    Don't do this. Use a *container* like a `list` or a `dict`. Don't dynamically create variables. – juanpa.arrivillaga Sep 23 '20 at 00:37
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Grismar Sep 23 '20 at 00:38
  • This is a common question that comes up frequently from new programmers - it's generally a bad idea, there's almost always better ways to solve a problem than self-modifying code. – Grismar Sep 23 '20 at 00:39

4 Answers4

3

you can create string and then use eval()

ex :

    eval(f"type{i} = Label(image=type_{type_list[i]})")
SeYeD.Dev
  • 271
  • 3
  • 4
  • Although technically correct, it's incredibly bad advice. Self modifying code is definitely not the best solution in this case. – Grismar Sep 23 '20 at 00:39
2

This is possible...

This is possible, using the built-in function eval, but it is almost always a very bad idea. Regardless, if you'd want to do this, you could do something like:

for i in range(0, len(type_list)):
    eval('type{} = something'.format(i))

But there are always better options

Instead, consider using a list of types:

types = []
for i in range(0, len(type_list)):
    types.append(something)

which you can then access like types[1].

If you want better "names" for the different types than just numbers, consider using a dict.

ADdV
  • 1,162
  • 2
  • 16
1

This is technically possible using Python's eval() function, but it's considered bad programming practice.

Instead of defining your images as type_Rock, type_Ground, I'd recommend creating a dictionary type_images where your key is Rock, Ground, etc.

Your loop would look like:

for t, img in type_images.items():
    type{i} = Label(image=img)
jrpear
  • 232
  • 2
  • 6
0

If I'm understanding your question correctly, there are a couple of routes to choose in my mind: one using classes and the other using a dictionary. For speed, I will show the method with a dictionary with an enumeration method to get by having to fiddle too much with indices.

data = {}
names = ["Ground", "Rock", "Water"]

for i, type in enumerate(type_list):
   key = names[i] 
   data[key] = Label(image=type)

From here, you can choose the data you want to work with by simply keying the name of the type you're interested in; for instance:

# type1
type1 = data["Ground"]

Please let me know if I misunderstood.