简体   繁体   English

Tkinter如何吹捧比较画布标签

[英]Tkinter how tout compare tags of canvas

Heyy, I have some issues with the tag system of Tkinter in Python 3. I have a matrix of canvas "tile" and some of them have the "trap" tag. 嘿,我在Python 3中使用Tkinter的标签系统遇到了一些问题。我有一个画布“ tile”矩阵,其中一些具有“ trap”标签。 How can I compare tag of each tiles of my matrix with the "trap" tag (I don't know how to write the condition, I know that I have to browse my matrix and compare each of them) 如何将矩阵的每个图块的标记与“陷阱”标记进行比较(我不知道如何写条件,我知道我必须浏览矩阵并比较每个矩阵)

Thanks 谢谢

The following compares the tags for each item in your canvas and returns a list of all the item ids that have the "trap" tag. 以下内容比较了画布中每个项目的标签,并返回具有“ trap”标签的所有项目ID的列表。

traps = [item for item in canvas.find_all() if "trap" in canvas.itemcget(item, "tags")]

Here, canvas.find_all() returns the id for all the items in the canvas. 在这里, canvas.find_all()返回画布中所有项目的ID。 canvas.itemcget gets the tags configuration information which is a list of tag names and we check to see if "trap" is a member of that list. canvas.itemcget获取标签配置信息,该信息是标签名称的列表,我们检查“ trap”是否为该列表的成员。

Generally you bind actions to the tags so for instance if you click an item with the trap tag you can perform some function. 通常,您将操作绑定到标签,例如,如果单击带有陷阱标签的项目,则可以执行某些功能。 If using bound events ( canvas.tag_bind ) then you may not need to search all the items in the above manner. 如果使用绑定事件( canvas.tag_bind ),则可能不需要以上述方式搜索所有项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM