如何制作乌龟's screen.listen()在执行函数时未侦听



在此示例代码中:

def some_function():
    //do something
   
screen.listen()
screen.onkey(some_function, "space")

如何使CCD_ 1在执行完CCD_?我试过这样做:

is_executing = False
def some_function():
    is_executing = True
    //do something
    is_executing = False
    
if not is_executing:
    screen.listen()
    screen.onkey(some_function, "space")

但它不起作用。有什么办法吗?

Turtle不允许您(故意(关闭screen.listen()。但在你描述的情况下,我所做的是:

def some_function():
    screen.onkey(None, 'space')
    # do something
    screen.onkey(some_function, 'space')
    
screen.onkey(some_function, 'space')
screen.listen()

其思想是,在some_function()执行时按下空格将在some_function()完成之前无效。这可以防止在事件处理程序执行时(重新(调用事件处理程序时发生的伪递归以及其他问题。

当调用turtle的textinput()numinput()方法时,您可能会发现自己无意中关闭了screen.listen(),因为它们将侦听器状态转移到弹出的输入窗口。

最新更新