为什么不执行 onscreenclick() 和 mainloop() 之间的语句?
Why don't statements between onscreenclick() and mainloop() get executed?
我正在尝试打印一个变量的值,该变量的值是根据 turtle 中单击点的坐标分配的。
import turtle as t
position=0
j=0
def get(a,b):
print("(", a, "," ,b,")")
global position
if b>0:
if a<0:
position=1
else:
position=2
else:
if a<0:
position=3
else:
position=4
def main():
global j
j = j+1
t.onscreenclick(get)
print(position)
t.mainloop()
main()
但是在 t.onscreenclick()
和 t.mainloop()
之间没有任何东西(我尝试过调用其他函数等)?
onscreenclick
不等待您的点击。它仅通知 mainloop
当您单击时它必须执行什么功能。 mainloop
做所有事情 - 它创建主 window,从操作系统获取 mouse/keyboard 事件,将事件发送到 window 中的 elements/widgets,运行分配给 onscreenclick
/ontimer
/等,在屏幕上重绘元素等
所以 print(position)
在甚至 mainloop
打开 window 之前执行。
你必须在里面打印 get()
import turtle as t
# --- functions ---
def get(x, y):
print("(", x, ",", y, ")")
if y > 0:
if x < 0:
position = 1
else:
position = 2
else:
if x < 0:
position = 3
else:
position = 4
print('pos:', position)
# --- main ---
# inform mainloop what function use when you click
t.onscreenclick(get)
# start "the engine" (event loop)
t.mainloop()
我正在尝试打印一个变量的值,该变量的值是根据 turtle 中单击点的坐标分配的。
import turtle as t
position=0
j=0
def get(a,b):
print("(", a, "," ,b,")")
global position
if b>0:
if a<0:
position=1
else:
position=2
else:
if a<0:
position=3
else:
position=4
def main():
global j
j = j+1
t.onscreenclick(get)
print(position)
t.mainloop()
main()
但是在 t.onscreenclick()
和 t.mainloop()
之间没有任何东西(我尝试过调用其他函数等)?
onscreenclick
不等待您的点击。它仅通知 mainloop
当您单击时它必须执行什么功能。 mainloop
做所有事情 - 它创建主 window,从操作系统获取 mouse/keyboard 事件,将事件发送到 window 中的 elements/widgets,运行分配给 onscreenclick
/ontimer
/等,在屏幕上重绘元素等
所以 print(position)
在甚至 mainloop
打开 window 之前执行。
你必须在里面打印 get()
import turtle as t
# --- functions ---
def get(x, y):
print("(", x, ",", y, ")")
if y > 0:
if x < 0:
position = 1
else:
position = 2
else:
if x < 0:
position = 3
else:
position = 4
print('pos:', position)
# --- main ---
# inform mainloop what function use when you click
t.onscreenclick(get)
# start "the engine" (event loop)
t.mainloop()