Python:等待鼠标输入或超时

Python:等待鼠标输入或超时,python,pygame,Python,Pygame,我正在建造一个摄影棚,在这个摄影棚中,我的目标是进行以下过程: (显示说明并等待鼠标按下OK区域)-->运行PHOTOBOOTH-->显示图片:打印图片或拍摄另一张照片(返回主菜单超时xx秒) 我在显示图片的一侧有以下代码,或者我暂停代码以查看图像,或者图像显示但没有捕捉到鼠标点击 def PreviewMontage(MontageFile): global LastTap LastTap = time.time() print("Session ID:", Sessi

我正在建造一个摄影棚,在这个摄影棚中,我的目标是进行以下过程: (显示说明并等待鼠标按下OK区域)-->运行PHOTOBOOTH-->显示图片:打印图片或拍摄另一张照片(返回主菜单超时xx秒)

我在显示图片的一侧有以下代码,或者我暂停代码以查看图像,或者图像显示但没有捕捉到鼠标点击

def PreviewMontage(MontageFile):
    global LastTap
    LastTap = time.time()
    print("Session ID:", SessionID)
    print("Show something.")
    preview = pygame.image.load(MontageFile)
    PILpreview = Image.open(MontageFile)
    previewSize = PILpreview.size # returns (width, height) tuple
    #added /1.5
    ScaleW = AspectRatioCalc(previewSize[0]/1.5, previewSize[1]/1.5, SCREEN_HEIGHT)
    preview = pygame.transform.scale(preview, (ScaleW, SCREEN_HEIGHT))
    SetBlankScreen()
    background.blit(preview, (SCREEN_WIDTH/2-ScaleW/2, 0))
    PrintScreen()
    #inserting conditions here - get mouse
    camera.stop_preview()
    UpdateDisplay()
    Wait()
    #sleep(20)
    return

def PrintScreen():
    #defines the text of the printscreen and buttons
    #insert button for printing 
    pygame.draw.rect(background, rgbGREEN, pygame.Rect(NEXT_X, 0, ZONEWIDTH, SCREEN_HEIGHT))
    #restarting button
    pygame.draw.rect(background, rgbRED, pygame.Rect(PREV_X, PREV_Y, ZONEWIDTH, SCREEN_HEIGHT))
    ##text
    Text = "Print or restart?"
    Text = smallfont.render(Text, 1, rgbRED)
    textpos = Text.get_rect()
    textpos.centerx = background.get_rect().centerx
    height = Text.get_height()
    background.blit(Text,(textpos)) #Write the small text
    return
# End of function.
我尝试过此代码以等待输入(仅接受触摸屏左侧或右侧的输入)-它正在工作,但似乎没有可靠的超时:

    def Wait():
clock = pygame.time.Clock()
waiting = True

while waiting:
    #clock.tick(time)
    time = 60
    #dt = clock.tick(30) / 1000  # Takes the time between each loop and convert to seconds.
    #time -= dt
    time = time -1 
    #print("Wainting..", waiting, "time ", time)
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFTMOUSEBUTTON:
            x, y = event.pos
            print("You pressed the left mouse button at (%d, %d)" % event.pos)
            LeftMouseButtonDown(x, y)


    if time == 0:
        waiting = False

return
时间变量不减。。。关于超时的概念有什么想法吗

这是“主循环”:

这里是鼠标检测(我无法使用预览功能):

def LeftMouseButtonDown(xx,yy):
#检测前一区域中的抽头

如果xx>=PREV_X和xx=NEXT_X和xx=START_MIN_X和yy>=START_MIN_Y和xx=UP_MIN_Y和xx=DOWN_MIN_Y和xx=LEFT_MIN_Y和xx=RIGHT_MIN_Y和xx我想你误解了pygame中事件的处理方式


如何修复
等待
功能
wait
方法产生错误的原因是
'int'对象不可编辑
,因为您正试图循环一个整数。事件中event.pos的
中的
event.type
是一个整数,表示事件的类型(从主循环读取
event
变量)。相反,您希望迭代(循环)自上次检查以来发生的所有事件。这可以通过调用pygame.event.get()中事件的事件队列:
来完成

然后,您需要检查该事件是否是您想要打破循环的事件类型。我不知道
KonamiCode('Left')
做了什么,但它们没有使用您迭代的事件,因此我怀疑它们是否有效。相反,如果event.type==x
其中
x
是要退出循环的事件类型,请选中

编辑:对更新函数的快速修复可能如下所示:

def wait(time):
    """
    Makes the program halt for 'time' seconds or until the user press the left mouse button.
    """
    clock = pygame.time.Clock()
    waiting = True

    while waiting:
        dt = clock.tick(30) / 1000  # Takes the time between each loop and convert to seconds.
        time -= dt
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFTMOUSEBUTTON:
                x, y = event.pos
                print("You pressed the left mouse button at (%d, %d)" % event.pos)
                LeftMouseButtonDown(x, y)

        if time <= 0:
            waiting = False

    return  # There is no need for explicit return
def等待(时间):
"""
使程序暂停“时间”秒或直到用户按下鼠标左键。
"""
clock=pygame.time.clock()
等待=真
等待时:
dt=时钟。滴答(30)/1000#表示每个循环之间的时间,并转换为秒。
时间-=dt
对于pygame.event.get()中的事件:
如果event.type==pygame.MOUSEBUTTONDOWN和event.button==LEFTMOUSEBUTTON:
x、 y=event.pos
打印(“您在(%d,%d)”%event.pos按了鼠标左键)
LeftMouseButtonDown(x,y)

如果时间允许,您如何创建等待输入的函数:只需尝试函数,更新帖子;)非常感谢,我更正了[wait]和[PreviewContent]函数,在[wait]函数中添加了[pygame.event.get()],并重构了[PreviewContent]函数。现在我唯一不能做的就是给用户一个与程序交互的时间限制,否则它会自动回到开始…你是什么意思?你能再解释一下,给我看看你的代码吗?是一个简单的网站,你可以只粘贴你的代码,保存和共享URL,或者如果你有它在Github上。或者,编辑您的问题以包含更新的代码。我想在PreviewMantage上添加一个超时,或者用户与程序交互(给定一定的时间),或者它将自身重置为空闲(开始屏幕)。完整代码:@did12345我已经查看了你的代码,我有点难以理解。相反,我更新了我的答案,以包含对等待函数的快速修复。你有两个问题:1。您在
while
循环中有
time=60
,这意味着您每次迭代都要重置时间。2. <如果
time
是一个浮点数(在本例中就是这样),则code>if time==0
不起作用。原因是浮点数很少是
0
,而是类似
0.0003525
-0.001124
的数字。因此,这种情况很少是真实的。而是检查when
if时间
def LeftMouseButtonDown(xx, yy):
    # Detect Taps in Previous Zone
    if xx >= PREV_X and xx <= ZONEWIDTH:
            TapPrev()
#     Detect Taps in Next Zone  
    if xx >= NEXT_X and xx <= SCREEN_WIDTH:
            TapNext()
    # Detect Taps in the Start Zone
    if xx >= START_MIN_X and yy >= START_MIN_Y and xx <= START_MAX_X and yy <= START_MAX_Y:
            TapStart()
    # Detect Taps in the Up Zone.
    elif xx >= UP_MIN_X and yy >= UP_MIN_Y and xx <= UP_MAX_X and yy <= UP_MAX_Y:
        KonamiCode('Up')
    # Detect Taps in the Down Zone.
    elif xx >= DOWN_MIN_X and yy >= DOWN_MIN_Y and xx <= DOWN_MAX_X and yy <= DOWN_MAX_Y:
        KonamiCode('Down')
    # Detect Taps in the Left Zone.
    elif xx >= LEFT_MIN_X and yy >= LEFT_MIN_Y and xx <= LEFT_MAX_X and yy <= LEFT_MAX_Y:
        KonamiCode('Left')
    # Detect Taps in the Right Zone.
    elif xx >= RIGHT_MIN_X and yy >= RIGHT_MIN_Y and xx <= RIGHT_MAX_X and yy <= RIGHT_MAX_Y:
        KonamiCode('Right')
    else:
        KonamiCodeReset()
        print("No Event")
    return
# End of function.
def wait(time):
    """
    Makes the program halt for 'time' seconds or until the user press the left mouse button.
    """
    clock = pygame.time.Clock()
    waiting = True

    while waiting:
        dt = clock.tick(30) / 1000  # Takes the time between each loop and convert to seconds.
        time -= dt
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFTMOUSEBUTTON:
                x, y = event.pos
                print("You pressed the left mouse button at (%d, %d)" % event.pos)
                LeftMouseButtonDown(x, y)

        if time <= 0:
            waiting = False

    return  # There is no need for explicit return