Python:等待鼠标输入或超时

时间:2016-12-27 09:49:22

标签: python pygame

我正在建立一个照相平台,在这个目标中我的目标是进行以下过程: (显示说明并等待,直到OK区域上的鼠标按下) - > RUN 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

Time变量不会减去...关于超时概念的任何想法?

这是"主循环":

while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = 0
    elif 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)
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_F4:
            print('F4 pressed, quitting.')
            QuitGracefully()

这是鼠标检测(我无法在我的预览功能上工作):

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.

1 个答案:

答案 0 :(得分:0)

我认为你误解了事件是如何在pygame中处理的。

如何修复wait功能

wait方法给出错误'int' object is not iterable的原因是因为您试图循环整数。 event.type中的for event.pos in event.type:是一个整数,表示它是什么类型的事件(从主循环中读取event变量)。相反,您希望迭代(循环)自上次检查以来发生的所有事件。这可以通过调用事件队列来完成:for event in pygame.event.get():

然后,您需要检查事件是否是您想要突破循环的事件类型。我不知道KonamiCode('Left')做了什么,但他们没有使用你迭代的事件,所以我怀疑他们会工作。相反,请检查if 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

提示

一些改进代码的提示。

  1. 在您的主循环中:event = pygame.event.poll()将仅返回事件队列中的单个事件。您可以改为调用pygame.event.get(),它将返回自上次调用以来的所有事件,然后遍历事件(如上所述和链接中所述。
  2. 尝试重构您的功能,这样他们一次只做一件简单的事情。例如,PreviewMontage加载图像,缩放图像,将其blit到背景,创建文本,blit文本,更新显示并等待30秒(或毫秒)。它有点多,使代码可重用性更低,可读性更低。
  3. 对变量和函数使用lower_case等通用命名约定,为类使用CamelCase,可以帮助其他读者更轻松地识别名称,从而更容易理解和阅读代码。
相关问题