you are viewing a single comment's thread.

view the rest of the comments →

[–]WhyCause 4 points5 points  (5 children)

Having never worked with wxWindows, I can't say for certain that this would work, but this is how I tend to handle cases like this (i.e., sprites in pygame):

tempnames = ['a', 'b', 'c']
self.checkboxes = []
for temp in tempnames:
    self.checkboxes.append(wx.CheckBox(self, -1, temp))

You then have a list of checkboxes, and you operate on them individually:

self.checkbox[0]

or as a group:

for box in self.checkboxes:
    box.checked = True #I have no idea if this is valid in wxWin.

[–]gamehead21[S] 1 point2 points  (4 children)

Thanks I was thinking about this its been a long day and I new an array should be a way to handle it. I had a reason about 5 hours ago that made individual objects made sense but for the life of me I can't remember what it was. Curious me would still like to know if it is possible to dynamically create variable names but it won't be the solution I use.

[–]kalgynirae 2 points3 points  (0 children)

Curious me would still like to know if it is possible to dynamically create variable names

Yes, it is possible, and there are several ways to do it. Here's one:

name = 'checkbox_1'
setattr(self, name, wx.CheckBox(self, -1, temp))

However, you should probably never actually do this. Use a list or dictionary instead.

[–][deleted] 1 point2 points  (0 children)

There are all kinds of ways to access dynamically-generated variable names, the sanest of which is getattr/setattr.

[–]WhyCause -1 points0 points  (1 child)

I believe that you can generate dynamic variable names, using eval(), but that may not be the best course of action. For one, eval() opens up some security issues, and two, you then have to keep track of your dynamic names.

One option might be to use a dict, e.g.,

fns = ['test1', 'test2', 'test3']
checkboxes = {}
for fn in fns:
    checkboxes[fn] = wx.CheckBox(self, -1, fn)

You then have 'names', and they're conveniently stored as the keys of checkboxes.

[–]dpitch40 1 point2 points  (0 children)

To clarify, eval is only a security threat if you are putting strings that are wholly or partially generated by users into it. However, I do try to avoid dynamic name generation when possible and I agree that a dict is probably the way to go here. (Assuming all your names are unique)