【发布时间】:2016-02-16 02:34:24
【问题描述】:
我有一个简单的 Python 程序,它使用感应帽操纵杆作为输入,它调用帽子上各种传感器的读数并将它们的值返回到 8x8 LED 显示屏。该程序有效,但只有一个问题,它返回每个 show_message() 两次。一次在初始按下时,当操纵杆“重置”时再次按下。我知道这一点,因为如果我举起它,它会显示温度,然后当我松开它时,它会再次显示。
谁能帮我只显示一次消息? pygame 事件似乎将操纵杆与箭头键绑定在一起,查看 pygame 文档我没有看到任何关于重置的提及。
这是我的代码:
from sense_hat import AstroPi
import time
import datetime
import pygame
from pygame.locals import *
ap = AstroPi()
ap.clear()
pygame.init()
pygame.display.set_mode((640,480))
pressure = 'P: ' + str(int(ap.get_pressure()))
temp = 'T: ' + str(int(ap.get_temperature_from_pressure()))
humidity = 'H: ' + str(int(ap.get_humidity()))
blah = 'blah!'
def handle_event(event):
if event.key == pygame.K_DOWN:
ap.show_message(pressure)
elif event.key == pygame.K_UP:
ap.show_message(temp)
elif event.key == pygame.K_LEFT:
ap.show_message(humidity)
elif event.key == pygame.K_RIGHT:
ap.show_message(blah)
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
handle_event(event)
if event.type == KEYUP:
handle_event(event)
if event.type == K_ESCAPE:
running = False
if event.type == pygame.QUIT:
running = False
【问题讨论】:
标签: python raspberry-pi pygame raspberry-pi2