Skip to content
Advertisement

Stopping running while loop if conditions are met

The code below is intended to print out the contents of arrays, until either the mouse (left click) is released, or the end of the array is reached. How would I stop the while loop in the function logging_mouse, once the mouse is released?

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()

Advertisement

Answer

you need to actually use the args to the on_click function… you can detect whether or not a button is pressed down or released, the signature for on_click should be on_click(x,y,button,pressed) where x and y represent the location of the mouse press, button says which button was pressed, and pressed is True if the button is down and False if it was released

Advertisement