有没有办法等待条件为真?

2024-09-28 01:25:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在用户单击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)

Tags: thetoimportfalsetimedefcanvasprint
3条回答
def handle_click(mouse_x,mouse_y):  # <== I think thats what ya need right dere, I dont think it knows you're clicking
    newpin = [[mouse_x,mouse_y], [0,0], 0,0,20, 1000000]

我试着在点击后放一条打印语句,但我甚至无法让它打印测试点击。这可能是我不够努力的缘故,tho;)我只记得用上面的方法来处理鼠标点击。(在我的情况下,它为弹球游戏创建了一个别针)如果你在turtle api中查找circle,你可以看到[0,0],0,0,20,100000]的意思

但最终,最后一个数字是10000,无论什么是“质量”,所以它移动的越少。又是我的处境。海龟。在屏幕上单击(点击手柄)。这至少是一个想法:)也是的,你可以在if之后等待。加入打印语句

Is it possible to continue running the turtle window after the lists have been created?

当你们和海龟的基于事件的模型战斗的时候,事情会变得很困难。使用模型,事情就会变得简单。下面的代码显示一个空白窗口,当您在三个位置单击它后,它将连接您的点以形成三角形:

from turtle import Screen, Turtle, mainloop

def getPosition(x, y):
    screen.onscreenclick(None)  # disable the handler inside the handler

    positions.append((x, y))

    if len(positions) == 3:
        screen.ontimer(listComplete)  # sometime after this function completes
    else:
        screen.onscreenclick(getPosition)  # restore the handler

def listComplete():
    for position in positions:
        turtle.goto(position)
        turtle.pendown()

    turtle.goto(positions[0])  # close our triangle

    # (Insert rest of code to follow)

positions = []

turtle = Turtle()
turtle.hideturtle()
turtle.penup()

screen = Screen()
screen.onscreenclick(getPosition)
mainloop()  # invoke as function to make Python 2 friendly as well

关键是“要遵循的其余代码”将出现在函数中,而不是顶级代码中

listcomplete=True内的getPos()不会更改全局变量,而是在局部范围内创建一个同名的新变量

要更改全局变量,必须告诉python从全局范围使用它:

def getPos(x,y):
    global listcomplete # tell python to use the variable from the global scope
    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

这是由于赋值运算符(=)的默认行为造成的

其他操作符,如比较操作符(==),如果在局部范围内找不到该变量,则将从封闭范围中查找该变量,因此您可以在main()内使用while listcomplete==False:,并告诉pyton使用全局范围中的变量


但理想情况下,您甚至不必使用该全局变量。相反,当您的条件满足时,运行turtle主循环并退出turtle窗口:

import turtle as t
canvas=t.getcanvas()
xlist=[]
ylist=[]

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:
        t.bye() # exit turtle window

t.onscreenclick(getPos)
t.Screen().mainloop() # will wait until turtle window is closed

print('list complete.')      ##Prints to alert the list has been finished
print(xlist)

相关问题 更多 >

    热门问题