Python Turtle 多次点击事件

Python Turtle Multiple Click Events

我正在尝试使用 Python 海龟制作 8 皇后拼图的海龟版本。

我已经开始了,但遇到了障碍,因为自定义 Turtle 对象上的点击事件似乎只触发一次。我知道屏幕点击事件会多次触发,这是 Turtle 实例的一个特性吗?请问我错过了什么?

import turtle

screen = turtle.Screen()
screen.reset()
SIZE = 40
screen.register_shape('box', ((-SIZE/2, SIZE/2), (SIZE/2, SIZE/2), (SIZE/2, -SIZE/2), (-SIZE/2, -SIZE/2)))
screen.register_shape('images/queenlogo40x40.gif')

class Box(turtle.Turtle):
    def __init__(self, x=0, y=0, place_color='green'):
        super(Box, self).__init__()
        self.place_color = place_color
        self.speed(0)
        self.penup()
        self.shape("box")
        self.color(place_color)
        self.setpos(x, y)
        self.has_queen = False
        self.onclick(self.click_handler)

    def click_handler(self, x, y):
        print("start:" , self.has_queen)

        if self.has_queen:
            self.shape('box')
            self.has_queen = False
        else:
            self.shape('images/queenlogo40x40.gif')
            self.has_queen = True

        print("end:" , self.has_queen)


    def __str__(self):
        """ Print piece details """
        return "({0}, {1}), {2}".format(self.xcor(), self.ycor(), self.place_color())

编辑:我可以通过将 self.onclick(self.click_handler) 添加到点击处理程序来解决这个问题,但这似乎是错误的。我确信我见过类似的功能,而无需在每次使用时重新绑定事件。

您的示例应该可以正常运行,我没有看到 概念性 问题。

但是 turtle 中有一个小故障。关于onclick的信息存储在海龟的_item 属性:

self.screen._onclick(self.turtle._item, fun, btn, add)

但是当您将海龟的形状从图像更改为多边形时,反之亦然,它会破坏 _item 属性:

if self._type in ["image", "polygon"]:
        screen._delete(self._item)

所以你的绑定丢失了。请注意,如果您更改行:

self.shape('images/queenlogo40x40.gif')

改为:

self.shape('turtle')

代码工作正常,因为您从一个多边形转到另一个多边形并且 _item 被保留。因此,在多边形和图像之间移动时,必须在形状更改后添加 self.onclick(self.click_handler)

我在下方稍微修改了您的代码以解决几个不相关的问题(例如修复了 super() 对 Python 3 的调用;删除了 __str__() 代码中不正确的括号。)

from turtle import Turtle, Screen

SIZE = 40

class Box(Turtle):
    def __init__(self, x=0, y=0, place_color='green'):
        super().__init__('box')

        self.speed('fastest')
        self.color(place_color)
        self.place_color = place_color
        self.has_queen = False
        self.penup()
        self.setpos(x, y)

        self.onclick(self.click_handler)

    def click_handler(self, x, y):

        print("start:", self.has_queen)

        if self.has_queen:
            self.shape('box')
        # else:
            # self.shape('turtle')
        else:
            self.shape('queenlogo40x40.gif')

        self.has_queen = not self.has_queen
        self.onclick(self.click_handler)  # redo since self.shape() may undo this

        print("end:", self.has_queen)

    def __str__(self):
        """ Print piece details """
        return "({0}, {1}), {2}".format(self.xcor(), self.ycor(), self.place_color)

screen = Screen()
screen.register_shape('box', ((-SIZE/2, SIZE/2), (SIZE/2, SIZE/2), (SIZE/2, -SIZE/2), (-SIZE/2, -SIZE/2)))
screen.register_shape('queenlogo40x40.gif')

tortoise = Box()

screen.mainloop()