[wxPython] TextCtrl incrimenting

Hi all:

Only been at this for a few weeks and have what is most likely a simple question but I can’t find the answer.

I’m trying to process a list in a for loop and insert the values into wxTextCtrl’s (Host1,Host2,etc…)

The closest I can come up with is:

    Which = 1

    for h in Hosts:
        F = "Host"
        Field = F+"%s" % Which
        self.Field.SetValue(h)
        Which = Which +1

I know it’s not working because Field is a string and self.Host1 is an object and Field, evidently is evaluated not for it’s content but literally, so my question is how can this be done without a mess of nested for loops for each field?

Thanks,

Rick

Why don't you just add the wxTextCtrl's to a list and parse it with
plain numbers??
like

self.FieldList =
#Of course you could do this with a for loop
self.FieldList.append(wxTextCtrl()) # your host1
self.FieldList.append(wxTextCtrl()) # your host2

#and then set the values
  Which = 0
  for h in Hosts:
    self.FieldList(Which).SetValue(h)
    Which = Which +1

or if for some reason you want to still use strings to refer to the
object you could use a dictionary....
self.FieldDict =
#Of course you could do this with a for loop
self.FieldDict.['host1'] = wxTextCtrl() # your host1
self.FieldDict.['host2'] = wxTextCtrl() # your host2

#and then set the values
  Which = 1
  F = "Host"
  for h in Hosts:
    Field = F + str(Which)
    self.FieldDict[Field].SetValue(h)
    Which = Which +1

if you are within a class, you can always use the built in dictionary
self.__dict__ which keeps every attribute of your class with the
variable name as a key. For instance in your case you can use...

self.__dict__['Host1'].SetValue(h) #instead of
self.'Host1'.SetValue(h)

with no problems (note that that you can use a string to refer to an
actual variable).
Actually, this would be a straight answer to your question, but I think
your code might be better organized implementing your own dictionary or
list.

I hope this helps you at all

Raul Cota

···

Richard Jones wrote:

Hi all:

Only been at this for a few weeks and have what is most likely a
simple question but I can't find the answer.

I'm trying to process a list in a for loop and insert the values
into wxTextCtrl's (Host1,Host2,etc...)

The closest I can come up with is:

        Which = 1
        for h in Hosts:
            F = "Host"
            Field = F+"%s" % Which
            self.Field.SetValue(h)
            Which = Which +1
I know it's not working because Field is a string and self.Host1 is an
object and Field, evidently is evaluated not for it's content but
literally, so my question is how can this be done without a mess of
nested for loops for each field?

Thanks,
Rick