如何让Turtle的screen.listen()在执行函数的时候不监听?
How to make Turtle's screen.listen() not listen when it is executing the function?
在此示例代码中:
def some_function():
//do something
screen.listen()
screen.onkey(some_function, "space")
如何让screen.listen()
在some_function
执行完之前不再执行?我试过这样做:
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()
执行时按 space 将不会生效,直到 some_function()
完成。这可以防止在执行时(重新)调用事件处理程序时发生的虚假递归,以及其他问题。
您会发现自己在调用 turtle 的 textinput()
和 numinput()
方法时无意中关闭了 screen.listen()
,因为它们将侦听器状态转移到弹出输入 windows.
在此示例代码中:
def some_function():
//do something
screen.listen()
screen.onkey(some_function, "space")
如何让screen.listen()
在some_function
执行完之前不再执行?我试过这样做:
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()
执行时按 space 将不会生效,直到 some_function()
完成。这可以防止在执行时(重新)调用事件处理程序时发生的虚假递归,以及其他问题。
您会发现自己在调用 turtle 的 textinput()
和 numinput()
方法时无意中关闭了 screen.listen()
,因为它们将侦听器状态转移到弹出输入 windows.