【发布时间】:2021-11-27 01:37:19
【问题描述】:
我正在尝试制作一个游戏,它会隐藏其后面的区域,直到您单击特定区域,但我不知道如何删除画布上的特定图像,因为我使用循环来创建图像并且无法为其设置特定标签。 PS我没有把完整的代码放在这个代码中,如果你运行它,我只需要知道在标签部分放什么。
`from tkinter import *
def cover():
b = 0
c = 0
for i in range(0,y):
for j in range(0,x):
s.create_image(b,c, anchor=NW, image=coverimg, tags="I don't know what to put in here")
b += 33
b = 0
c += 33
def reveal(event):
for i in range(0,y):
for j in range(0,x):
s.delete("I don't know what to put in here")
s.bind("<Button-1>", reveal)`
【问题讨论】:
-
您可以先将图像存储在某个位置(在数组中),然后从数组中访问它们
-
将图像保存在列表或字典中。
-
无论您是否使用标签来识别图像,您仍然必须使用列表或字典,正如前两个 cmets 建议的那样。最好简单地使用图像的 ID 而不是标签来识别它们。
-
对于您的情况,您可以使用
f"img_{y}_{x}"之类的东西作为标签。