[英]Why my code doesn't work?
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. 最后一个返回一个与
i
相同名称的wxpython对象,从而打破了循环。 So I need to find a way of returning the name of this object. 因此,我需要找到一种返回该对象名称的方法。 But
i.__name__
doesn't work. 但是
i.__name__
不起作用。
As the traceback says you have a KeyError
. 如回溯所示,您有一个
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. 因为当执行
str(i)
时i
是一个对象,所以您会得到"<wx._core.MenuItem; proxy of <Swig Object of type 'wxMenuItem *' at 0x165aeab0> >"
,因此在VarFiles
容器中不存在这样的键。
It has nothing whatsoever to do with the for loop or the way you write your list. 它与for循环或编写列表的方式无关。
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)
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.