Python多个onclick事件

时间:2019-05-05 18:43:59

标签: python turtle-graphics

我希望能够同时具有这两个turtle onclick事件功能,但只有其中之一起作用。我有一个函数可以在用户单击的位置绘制一个正方形,并且有一个关闭按钮,可在您单击该程序时将其关闭。这些功能一次只能起作用。

import turtle
import math
turtle.penup()
def square(x, y):
    turtle.up()
    turtle.goto(x, y)
    turtle.down()
    for i in range(4):
        turtle.forward(50)
        turtle.left(90)



def closebutton(location1):
(x,y) = location1
turtle.up()
turtle.setposition(location1)
turtle.down()
for i in range(2):
    turtle.forward(40)
    turtle.left(90)
    turtle.forward(25)
    turtle.left(90)
turtle.up()
turtle.forward(7.5)
turtle.left(90)
turtle.forward(5)
turtle.right(90)
turtle.write("close")

def btnclick(x, y):
    if x > 100 and x < 141 and y > -100 and y < -75:
        quit()
turtle.onscreenclick(btnclick)


closebutton((100,-100))
turtle.onscreenclick(square)

2 个答案:

答案 0 :(得分:0)

def square(x, y):
    # Check area is Close button
    if x > 100 and x < 141 and y > -100 and y < -75:
        quit()

    turtle.up()
    turtle.goto(x, y)
    turtle.down()
    for i in range(4):
        turtle.forward(50)
        turtle.left(90)

我认为,在square功能下,我们可以检查关闭按钮的区域

答案 1 :(得分:0)

我们可以按照您尝试的方式解决此问题,但是我们必须按特定顺序添加屏幕点击处理程序,并利用很少使用的add参数:

from turtle import Screen, Turtle, mainloop

FONT_SIZE = 20
FONT = ('Arial', FONT_SIZE, 'normal')

def draw_square(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

    for _ in range(4):
        turtle.forward(50)
        turtle.left(90)

def button_clicked(x, y):
    if 100 < x < 100 + text_width and -FONT_SIZE/2 - 100 < y < FONT_SIZE/2 - 100:
        quit()

def make_close_button(x, y):
    turtle.penup()

    turtle.setposition(x, y - FONT_SIZE / 2)
    turtle.write("close", move=True, font=FONT)
    width = turtle.xcor() - x  # pixel width of text we just wrote

    turtle.pendown()

    for _ in range(2):
        turtle.left(90)
        turtle.forward(FONT_SIZE)
        turtle.left(90)
        turtle.forward(width)

    turtle.penup()
    turtle.home()

    return width

turtle = Turtle()

text_width = make_close_button(100, -100)

screen = Screen()
screen.onscreenclick(button_clicked)
screen.onscreenclick(draw_square, add=True)

mainloop()

但是这种方法使得很难在事件处理程序代码中禁用onscreenclick()事件。也就是说,尽管仍在绘制一个正方形,但是您可以单击屏幕上的其他位置并开始第二个,从而干扰了第一个正方形的完成。为了解决这个问题,我们可能会尝试使用一个单独的事件处理函数来使用一种完全不同的方法,该函数可以根据需要禁用和重新启用:

from turtle import Screen, Turtle, mainloop

FONT_SIZE = 20
FONT = ('Arial', FONT_SIZE, 'normal')

def draw_square():
    for _ in range(4):
        turtle.forward(50)
        turtle.left(90)

def button_clicked(x, y):
    screen.onscreenclick(None)  # disable event handler inside event handler

    if button.distance(x, y) < FONT_SIZE:
         quit()

    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

    draw_square()

    screen.onscreenclick(button_clicked)

def make_close_button(x, y):
    button = Turtle(visible=False)
    button.speed('fastest')
    button.penup()

    button.setposition(x, y - FONT_SIZE / 2)
    button.write("close", move=True, font=FONT)
    width = button.xcor() - x  # pixel width of text we just wrote

    button.pendown()

    for _ in range(2):
        button.left(90)
        button.forward(FONT_SIZE)
        button.left(90)
        button.forward(width)

    button.penup()
    button.setposition(x + width / 2, y)

    return button

button = make_close_button(100, -100)

turtle = Turtle()

screen = Screen()
screen.onscreenclick(button_clicked)

mainloop()

在这里,我们对功能进行了一些分解,以使功能具有特定职责。而且,除了弄清楚我们是否单击按钮之外,我们还留下了一只乌龟,只是检查了与那只乌龟的点击距离。

两种方法都有其优点和缺点。