0

Okay. So I searched a good while now and couldn't find another questions which describes my problem.

When I want to define a variable in Python, easy:

var = value

When I'm inside a class and want to define it for the whole class, easy:

self.var = value

In my special case I have a list which contains a few strings. I want to create for each of these strings a variable, but for I am in a GUI function, I want to make it the above described way with self.

My problem now is, that when I try to put each string into self.string like:

list = [string, string1, string2]

for string in list:
    self.string = QPushButton()

Python does not compile the string in self.string but takes it literally. Therefore only three same variables all called self.string assigned with the method QPushButton are created.

What do I have to do to get three objects:

self.string = QPushButton()
self.string1 = QPushButton()
self.string2 = QPushButton()

To create some context: I want to create these buttons dynamically, each time a new item is added to the list, a new button is created. The list items are camera paths for an OpenCV video surveillance system.

Of course further arguments are used (setGeometry, setText, etc.) I just excluded them for readability.

My idea: It should be possible to pass an index into the for loop which then is added to the variable name: self.string + index = ... or something like .format()

Thanksalot

Aaroknight
  • 53
  • 10

0 Answers0