【发布时间】:2020-04-23 01:47:26
【问题描述】:
我一直在使用 tkinter for gui 开发我的蛇游戏。问题出在其中一个函数上。
该函数应该分别使用 Canvas().create_rectangle 和 Canvas().create_oval 绘制身体碎片和水果。因此,我没有为每种情况编写单独的代码,而是决定只编写一次,然后使用“形状”参数对其进行修改,该参数可以是“矩形”或“椭圆形”。函数还必须返回已绘制元素的 id 以用于其自身目的。原来那部分代码是这样的:
exec(
"""
segment_id = self.grid.create_{}(coords,
[coord + self.pxSize for coord in coords],
fill=color, tag=tag, width=0)
""".format(shape))
return segment_id
我得到了NameError: name 'self' is not defined,而不是普通的NameError: name 'segment_id' is not defined。
谷歌搜索后,我只发现了这个:
ldict = {}
exec('var = something', globals(), ldict)
var = ldict['var']
解决了NameError: name 'segment_id' is not defined,但没有解决另一个问题。因此,使用科学的 poke 方法,我通过将 locals() 传递给它的“globals”参数来修复它。它有效,现在我更加困惑了。
代码如下:
class Game(Tk):
def __init__(self):
...
# ...
def drawSegment(self, position, color, tag, shape, id_=None):
coords = self.field.from_1D_to_2D(position)
coords = [i * self.pxSize for i in coords]
# id > 1, otherwise deletes background
if id_ and id_ > 1:
self.grid.delete(id_)
# ???
ldict = {}
exec(
"""
segment_id = self.grid.create_{}(coords,
[coord + self.pxSize for coord in coords],
fill=color, tag=tag, width=0)
""".format(shape), locals(), ldict)
segment_id = ldict['segment_id']
return segment_id
# ...
我需要的是关于为什么会起作用以及发生了什么的答案。
【问题讨论】:
-
为什么要这样使用
exec? -
抱歉,我不明白这与
exec有什么关系。请提供更完整的代码sn-p,以及完整的错误信息,以便我们了解更多。 -
你知道
globals()和locals()是什么意思吗? -
至少你可以写
segment_id = getattr(self.grid, 'create_{}'.format(shape))(coords, ...)。不要使用exec来避免普通查找。