I'm writing a code using Python 3.8 where the user is asked to press a button as soon as an image appears. The image is then replaced with a white screen for a random interval of 1 to 4 seconds, until a new image appears. I used cv2.waitKey() until the spacebar gets pressed: no problem, works just fine. See below:
    import sys, cv2, random
    im = cv2.imread('image.tiff')                    
    imBlank = cv2.imread('imageBlank.tiff')            
    cv2.namedWindow('win', cv2.WINDOW_NORMAL) 
    for i in range(5):           # create a loop that runs 5 times
        cv2.imshow('win', im)    # show the image
        k = cv2.waitKey(0)       # wait until key press           
        if k == 32:              # when space bar is pressed
            cv2.imshow('win', imBlank)  # show a blank screen
            SI = random.randint(1,4)    # random interval between 1 and 4 seconds
            cv2.waitKey(SI*1000)        # wait for random interval until loop starts again
Now the problem is that I want to do exactly this, but instead of the space bar, I want the user to press a mouse button. For as far as I can find, the cv2.waitKey() function does not work with the mouse, so I wrote the code below:
    def showBlank(event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            cv2.imshow('win', imBlank)
        elif event == cv2.EVENT_RBUTTONDOWN:
            cv2.imshow('win', imBlank)              
            
    # repeat the loop 5 times
    for i in range(5):
        cv2.imshow('win', im)                       # show the image
        cv2.setMouseCallback('win', showBlank)      # go to function showBlank()
        """ wait here until mouse button is clicked"""
        SI = random.randint(1,4)                    # random interval between 1 and 4 seconds
        cv2.waitKey(SI*1000)                        # wait for random interval until loop starts again
Again, this code basically works, but if the user does not press the mouse button, the loop keeps running anyways. I want the loop to wait for the random interval to start until the mouse is clicked.
Thanks! I hope someone can help me out here
 
     
    