等待Python Turtle中的点击

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

我想等待 Python Turtle 中的点击,然后再继续。代码概要如下:

do_stuff()

# Wait for click and DO NOT continue if user has not clicked

# When user clicks:

do_other_stuff()

end()
SOF 上的

This 答案提出了类似的问题,但提出的解决方案涉及扩展 Turtle 类。我记得用了一种更简单的方法来做到这一点,但记不起了。

任何帮助将不胜感激。

python turtle-graphics
2个回答
1
投票

在不修改海龟或魔法的情况下,您可能可以使用 tirtle.onclick

do_stuff()
# Wait for click and DO NOT continue if user has not clicked
# When user clicks:
def second_part():
    turtle.onclick(None)
    do_other_stuff()
    end()
turtle.onclick(second_part)
turtle.mainloop()

您甚至可以手动链接它们(未经测试):

do_stuff()

def third_part():
    turtle.onclick(None)
    ...

def second_part():
    turtle.onclick(third_part)
    ...

turtle.onclick(second_part)
...

0
投票

将这些行放在脚本末尾对我来说效果很好

 def screenclick(x, y):
        出口()
       
    海龟.onscreenclick(屏幕点击)
    乌龟.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.