[英]How to run two onclick functions independently (Python with turtle)
I have two onclick functions in my program, I want the first one to run 3 times and then stop permanently, and then the second one to run 5 times and stop permanently.我的程序中有两个 onclick 函数,我希望第一个运行 3 次然后永久停止,然后第二个运行 5 次并永久停止。 How could I do that?
我怎么能那样做? I tried to use a counter to count the number of clicks but I can't figure out how to make that work.
我尝试使用计数器来计算点击次数,但我不知道如何进行。
Here is my code:这是我的代码:
import turtle
import random
wn = turtle.Screen()
wn.bgcolor('lightblue')
svea = turtle.Turtle()
sample=[1,2,3]
click_count=0
def square(t,size,r,g,b,x,y,z,click_count):
if click_count==3:
quit()
else:
t.pencolor(x,y,z)
t.begin_fill()
for i in range(4):
t.forward(size)
t.right(90)
t.fillcolor(r,g,b)
t.end_fill()
def triangle(t,size,r,g,b,x,y,z):
t.pencolor(x,y,z)
t.begin_fill()
for i in range(3): # repeat four times
t.forward(size)
t.left(120)
t.fillcolor(r,g,b)
t.end_fill()
def rectangle(t,size,r,g,b,x,y,z):
t.pencolor(x,y,z)
t.begin_fill()
for i in range(2): # repeat four times
t.forward(size)
t.left(90)
t.forward(size*1.25)
t.left(90)
t.fillcolor(r,g,b)
t.end_fill()
def drawOnClick(x,y):
wn.onclick(None)
size=random.randrange(50,150)
svea.up()
svea.goto(x,y)
svea.down()
square(svea,size,random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randint(0,3))
triangle(svea,size,random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255))
svea.up()
svea.goto(x+(size*0.4),y-size)
svea.down()
rectangle(svea,size/4,random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255))
svea.up()
svea.goto(x+(size*0.15),y-(size*0.15))
svea.down()
square(svea,size/5,random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),2)
svea.up()
svea.goto(x+(size*0.65),y-(size*0.15))
svea.down()
square(svea,size/5,random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),random.randrange(0,255),2)
wn.onclick(drawOnClick)
#click_count+=1
#if click_count==3:
#quit()
def drawOnClick2(x,y):
size=random.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)
#wn.onclick(drawOnClick2)
wn.onclick(drawOnClick)
#wn.onclick(None)
# wn.exitonclick()
I'm going to suggest a simple list of event handler functions that you pop()
each time you restore the event handler at the end of the event handler function.我将推荐一个简单的事件处理函数列表,您每次在事件处理函数结束时恢复事件处理函数时都会
pop()
这些函数。 See onclick_functions
in the following rewrite of your code:请参阅以下代码重写中的
onclick_functions
:
from turtle import Screen, Turtle
from random import randrange
def square(t, size, r, g, b, x, y, z):
t.color((x, y, z), (r, g, b))
t.begin_fill()
for _ in range(4):
t.forward(size)
t.right(90)
t.end_fill()
def triangle(t, size, r, g, b, x, y, z):
t.color((x, y, z), (r, g, b))
t.begin_fill()
for _ in range(3): # repeat three times
t.forward(size)
t.left(120)
t.end_fill()
def rectangle(t, size, r, g, b, x, y, z):
t.color((x, y, z), (r, g, 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, size, randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255))
triangle(svea, size, randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255))
svea.up()
svea.goto(x + size * 0.4, y - size)
svea.down()
rectangle(svea, size/4, randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255))
svea.up()
svea.goto(x + size * 0.15, y - size * 0.15)
svea.down()
square(svea, size/5, randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255))
svea.up()
svea.goto(x + size * 0.65, y - size * 0.15)
svea.down()
square(svea, size/5, randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 255), randrange(0, 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()
This should make it simple to change the number of calls to each event handler function and add new ones.这应该使更改对每个事件处理程序函数的调用次数和添加新函数变得简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.