python 3变量在另一个变量的名称中使用?
在 python 3(使用 tkinter)中,我有以下图像:
SqImg1 = PhotoImage(file='SqImg1.gif')
SqImg2 = PhotoImage(file='SqImg2.gif')
SqImg3 = PhotoImage(file='SqImg3.gif')
SqImg4 = PhotoImage(file='SqImg4.gif')
...
我还有一个函数需要根据变量返回这些图像之一。所以如果函数确定 var 为 1,它需要返回 SqImg1...
def returnImage():
list = ['ale', 'boo', 'boo', 'cat', 'doe', 'boo', 'eel']
var = 0
for item in list:
if item == 'boo':
var += 1
return SqImg[var]
我希望上面返回 SqImg3
这可能很简单,但我似乎无法通过谷歌找到我正在寻找的内容。
in python 3 (with tkinter) i have the following images:
SqImg1 = PhotoImage(file='SqImg1.gif')
SqImg2 = PhotoImage(file='SqImg2.gif')
SqImg3 = PhotoImage(file='SqImg3.gif')
SqImg4 = PhotoImage(file='SqImg4.gif')
...
i also have a function that needs to return one of those images based on a variable. so if the function determines var to be 1, it needs to return SqImg1...
def returnImage():
list = ['ale', 'boo', 'boo', 'cat', 'doe', 'boo', 'eel']
var = 0
for item in list:
if item == 'boo':
var += 1
return SqImg[var]
i would want the above to return SqImg3
this is probably simple, but i can't seem to find what im looking for with google.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的错误马上就出现了:
应该是:
或者甚至是:
There's your mistake straight away:
should be:
or even:
两个选项
d = {}; d["boo"] = img3
return eval("sqlimg%d" % var)
第一个选项可能是除非你有特殊情况,否则你正在寻找
Two options
d = {}; d["boo"] = img3
return eval("sqlimg%d" % var)
The first option is probably what you're looking for unless you have special circumstances