如果满足条件,则停止 运行 while 循环

Stopping running while loop if conditions are met

下面的代码旨在打印出数组的内容,直到松开鼠标(左键单击)或到达数组的末尾。释放鼠标后,如何停止函数 logging_mouse 中的 while 循环?

running = False
i = 0
delta_x=[1,2,3]
delta_y = [3,2,1]

def logging_mouse(running,i):
    while (running and i < len(delta_x)):
       print(delta_x[i],delta_y[i])
       i = i+1
    running = False

def on_click(*args):
    global running
    global i
    print(running)
    i = args[3]
    print(i)
    if args[-1]:
        if not running:
            running = True
            threading.Thread(target=logging_mouse(running,i)).start()
    else:
        running = False
        i = 0

with Listener(on_click=lambda event1,event2,event3,event4: on_click(event1,event2,event3,i,event4)) as listener:
        listener.join()

你需要实际使用 argson_click 功能...你可以检测按钮是否被按下或释放,on_click 的签名应该是 on_click(x,y,button,pressed) 其中 xy 表示鼠标按下的位置, button 表示按下了哪个按钮, pressedTrue 如果按钮已按下,False 如果已释放