在‘turtle’中点击形状时绑定事件

问题描述 投票:0回答:1

我在屏幕上生成随机数字。我希望当我单击任何圆形形状时屏幕会清除。如何在点击圆圈时绑定事件?我尝试了

onclick()
clearscreen()
,但不起作用。 我的代码:

# -*- coding: utf-8 -*-
import random
import turtle
class Figure:
    def __init__(self):
        __colors = ['red', 'green', 'yellow', 'purple', 'orange']
        __figures = ['square', 'circle', 'triangle']
        self.__x = random.randint(-330, 330)
        self.__y = random.randint(-230, 230)
        self.__color = random.choice(__colors)
        self.t = turtle.Turtle()
        self.t.hideturtle()
        self.t.fillcolor(self.__color)
        self.t.up()
        self.t.setpos(self.__x, self.__y)
        self.t.down()
        __choose = random.choice(__figures)
        if __choose == 'square':
            self.square()
        elif __choose == 'circle':
            self.circle()
        else:
            self.triangle()


    def square(self):
        self.t.begin_fill()
        for i in range(4):
            self.t.fd(50)
            self.t.left(90)
        self.t.end_fill()


    def circle(self):
        self.t.begin_fill()
        self.t.circle(50)
        self.t.end_fill()
        self.t.onclick(self.delete_figures)
        
    def triangle(self):
        self.t.begin_fill()
        self.t.fd(50)
        self.t.lt(120)
        self.t.fd(50)
        self.t.lt(120)
        self.t.fd(50)
        self.t.end_fill()

    def delete_figures(self, x ,y):
        turtle.clearscreen()



turtle.tracer(1, 0)

for i in range(10):
    Figure()


turtle.mainloop()

我希望在单击任何圆形形状时清除屏幕。如何在点击圆圈时绑定事件?

python turtle-graphics python-turtle
1个回答
0
投票

onclick
不会在海龟制作的绘图上注册点击侦听器,仅在海龟本身上注册。但这些海龟是隐藏的,即使可见也只有几个像素大,所以没有什么可点击的。

有两个主要选项:

  1. 保持海龟绘图相同并添加屏幕点击监听器。在单击处理程序中,循环遍历所有圆并确定单击的 x/y 位置是否发生在任何圆的半径内。这是一个例子:

    import random
    import turtle
    
    
    class Figure:
        def __init__(self):
            __colors = ['red', 'green', 'yellow', 'purple', 'orange']
            __figures = ['square', 'circle', 'triangle']
            self.__x = random.randint(-330, 330)
            self.__y = random.randint(-230, 230)
            self.__color = random.choice(__colors)
            self.t = turtle.Turtle()
            self.t.hideturtle()
            self.t.fillcolor(self.__color)
            self.t.up()
            self.t.setpos(self.__x, self.__y)
            self.t.down()
            __choose = random.choice(__figures)
    
            if __choose == 'square':
                self.square()
            elif __choose == 'circle':
                self.circle()
            else:
                self.triangle()
    
        def square(self):
            self.t.begin_fill()
    
            for i in range(4):
                self.t.fd(50)
                self.t.left(90)
    
            self.t.end_fill()
    
        def circle(self):
            self.t.begin_fill()
            self.t.circle(50)
            self.t.end_fill()
            self.t.right(90)
            self.t.forward(-50)
    
        def triangle(self):
            self.t.begin_fill()
            self.t.fd(50)
            self.t.lt(120)
            self.t.fd(50)
            self.t.lt(120)
            self.t.fd(50)
            self.t.end_fill()
    
    
    turtle.tracer(1, 0)
    figures = []
    
    for i in range(10):
        figures.append(Figure())
    
    def test_circle_click(x, y):
        for f in figures:
            if f.t.distance(x, y) < 50:
                turtle.clearscreen()
    
    turtle.onscreenclick(test_circle_click)
    turtle.mainloop()
    
  2. 不要画圆形,而是使用

    turtle.shape("circle")
    turtle.color()
    让乌龟看起来像一个圆形。不要隐藏海龟以允许使用当前处理程序单击它。 这是一个例子

© www.soinside.com 2019 - 2024. All rights reserved.