views:

126

answers:

3

Why doesn't this work?

for i in [a, b, c]:
    i.SetBitmap(wx.Bitmap(VarFiles[str(i)]))

I get:

Traceback (most recent call last):
  File "<string>", line 11, in ?
  File "codecc.py", line 724, in ?
    app = MyApp(0)   # stdio to console; nothing = stdio to its own window
  File "C:\Program Files (x86)\WorldViz\Vizard30\bin\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7978, in __init__
    self._BootstrapApp()
  File "C:\Program Files (x86)\WorldViz\Vizard30\bin\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7552, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "codecc.py", line 719, in OnInit
    frame = VFrame(parent=None)
  File "codecc.py", line 374, in __init__
    i.SetBitmap(wx.Bitmap(VarFiles[str(i)]))
KeyError: "<wx._core.MenuItem; proxy of <Swig Object of type 'wxMenuItem *' at 0x165aeab0> >"

Interestingly, this works:

 i.SetBitmap(wx.Bitmap(VarFiles["i"])) 

but this doesn't:

 i.SetBitmap(wx.Bitmap(VarFiles[i]))

The last one returns an wxpython object with the same name as i, thus breaking the loop. So I need to find a way of returning the name of this object. But i.__name__ doesn't work.

+1  A: 

As the traceback says you have a KeyError. Since i is an object when you do str(i) you get "<wx._core.MenuItem; proxy of <Swig Object of type 'wxMenuItem *' at 0x165aeab0> >", such key doesn't exist in a VarFiles container.

It has nothing whatsoever to do with the for loop or the way you write your list.

SilentGhost
So, what should I use instead of str(i) to get a, b and c instead of "<wx._core.MenuItem; proxy of <Swig Object of type 'wxMenuItem *' at 0x165aeab0> >" ?
relima
@relima: well, what's inside `VarFiles`? how are those things related to the `i` instances?
SilentGhost
VarFiles is a dictionary which returns the path for an image when a, b and or c are given to it.VarFiles[a] returns: ./path/to/img/image.pngit works well everywhere but in this loop.
relima
@relima: so why don't you give `i` to it? why do you give `str(i)` to it?
SilentGhost
@SilentGhost: it gives the same error... for some reason, "a" works but str(a) returns this other object that has been instantiated.
relima
+1  A: 

Break it down using a single case. Where is the error in this?

s = str(a)
v = VarFiles[s]
w = wx.Bitmap(v)
a.SetBitmap(w)
hughdbrown
If I do: s = "a" , it works. If I do: s = str(a) , it doesn't and I get: KeyError: "<wx._core.MenuItem; proxy of <Swig Object of type 'wxMenuItem *' at 0x165924b0> >"
relima
I see above that @SilentGhost has elicited that `VarFiles` is a dictionary. So what should the key to `VarFiles` be in this instance? Show us what any of a, b, or c is and then show us the construction of the VarFiles dictionary.
hughdbrown
relima
A: 

This is how I """"fixed"""" my code:

    list_a = [a, b, c]
    list_b = ["a", "b", "c"]
    [i.SetBitmap(wx.Bitmap(VarFiles[list_b[list_a.index(i)]])) for i in list_a]
relima
http://docs.python.org/library/functions.html#zip
SilentGhost
Thank you SilentGhost, this zip function helps me with this and some other things I have been trying to do!
relima