我试图在用户单击turtle屏幕时分别记录3次坐标,然后在完成后继续运行其他命令。点击3次没有任何作用,shell会一直打印它正在等待,而再点击一次会导致整个事情无法进行,我会从turtle图形窗口收到一条“无响应”消息
import turtle as t
import time
canvas=t.getcanvas()
xlist=[]
ylist=[]
listcomplete=False
def getPos(x,y):
xlist.append(canvas.winfo_pointerx()) ##Logs the x and y coords when mouse is clicked
ylist.append(canvas.winfo_pointery())
print('appended the lists.')
if len(xlist)==3:
listcomplete=True
t.onscreenclick(getPos)
def main():
while listcomplete==False:
time.sleep(1)
print('waiting...') ##Prints periodically just to let me know it's still running
main()
print('list complete.') ##Prints to alert the list has been finished
print(xlist)
(Insert rest of code to follow)
我试着在点击后放一条打印语句,但我甚至无法让它打印测试点击。这可能是我不够努力的缘故,tho;)我只记得用上面的方法来处理鼠标点击。(在我的情况下,它为弹球游戏创建了一个别针)如果你在turtle api中查找circle,你可以看到[0,0],0,0,20,100000]的意思
但最终,最后一个数字是10000,无论什么是“质量”,所以它移动的越少。又是我的处境。海龟。在屏幕上单击(点击手柄)。这至少是一个想法:)也是的,你可以在if之后等待。加入打印语句
当你们和海龟的基于事件的模型战斗的时候,事情会变得很困难。使用模型,事情就会变得简单。下面的代码显示一个空白窗口,当您在三个位置单击它后,它将连接您的点以形成三角形:
关键是“要遵循的其余代码”将出现在函数中,而不是顶级代码中
listcomplete=True
内的getPos()
不会更改全局变量,而是在局部范围内创建一个同名的新变量要更改全局变量,必须告诉python从全局范围使用它:
这是由于赋值运算符(
=
)的默认行为造成的其他操作符,如比较操作符(
==
),如果在局部范围内找不到该变量,则将从封闭范围中查找该变量,因此您可以在main()
内使用while listcomplete==False:
,并告诉pyton使用全局范围中的变量但理想情况下,您甚至不必使用该全局变量。相反,当您的条件满足时,运行turtle主循环并退出turtle窗口:
相关问题 更多 >
编程相关推荐