如何独立运行两个onclick函数带有turtle的Python

问题描述

我的程序中有两个onclick函数,我希望第一个函数运行3次然后永久停止,然后让第二个函数运行5次并永久停止。我该怎么办?我试图使用一个计数器来计算点击次数,但我不知道该如何进行。

这是我的代码

.is-dropdown-submenu-parent > a:after {
    display:none!important;
}

解决方法

我将为您提供一个简单的事件处理函数列表,您每次pop()都会在事件处理函数的末尾恢复事件处理函数。请参见以下代码重写中的onclick_functions

from turtle import Screen,Turtle
from random import randrange

def square(t,size,r,g,b,x,y,z):
    t.color((x,z),(r,b))
    t.begin_fill()

    for _ in range(4):
        t.forward(size)
        t.right(90)

    t.end_fill()

def triangle(t,b))
    t.begin_fill()

    for _ in range(3):  # repeat three times
        t.forward(size)
        t.left(120)

    t.end_fill()

def rectangle(t,b))
    t.begin_fill()

    for _ in range(2):  # repeat two times
        t.forward(size)
        t.left(90)
        t.forward(size * 1.25)
        t.left(90)

    t.end_fill()


def drawOnClick(x,y):
    screen.onclick(None)

    size = randrange(50,150)
    svea.up()
    svea.goto(x,y)
    svea.down()

    square(svea,randrange(0,255),255))
    triangle(svea,255))

    svea.up()
    svea.goto(x + size * 0.4,y - size)
    svea.down()

    rectangle(svea,size/4,255))

    svea.up()
    svea.goto(x + size * 0.15,y - size * 0.15)
    svea.down()

    square(svea,size/5,255))

    svea.up()
    svea.goto(x + size * 0.65,255))

    if onclick_functions:
        screen.onclick(onclick_functions.pop(0))

def drawOnClick2(x,y):
    screen.onclick(None)

    size = randrange(10,20)

    svea.begin_fill()
    svea.up()
    svea.goto(x,y)
    svea.down()

    svea.circle(size)

    svea.right(90)
    svea.forward(size * 2)
    svea.right(45)
    svea.forward(size * 2)
    svea.right(180)
    svea.forward(size * 2)
    svea.right(90)
    svea.forward(size * 2)
    svea.right(180)
    svea.forward(size * 2)
    svea.right(45)
    svea.forward(size)
    svea.right(90)
    svea.forward(size * 1.5)
    svea.right(180)
    svea.forward(size * 3)
    svea.right(180)

    if onclick_functions:
        screen.onclick(onclick_functions.pop(0))

onclick_functions = [drawOnClick] * 3 + [drawOnClick2] * 5

screen = Screen()
screen.bgcolor('lightblue')
screen.colormode(255)

svea = Turtle()

if onclick_functions:
    screen.onclick(onclick_functions.pop(0))

screen.mainloop()

这应该使更改每个事件处理函数的调用数量和添加新的函数变得简单。