我在我的Raspberry Pi上运行了一个python脚本,它使用了一个按钮和python中断。当我使用os.system(' clear')时,我遇到了问题。每当我在代码中包含它时,中断就会停止工作。这是一个示例,显示按下按钮按下4次按下,然后在使用os.system(' clear')时停止。知道为什么吗?
import os
import time
import sys
from datetime import datetime
import RPi.GPIO as GPIO
prev_time = ""
prev_counter = 0
counter = 0
# Setup GPIO, button, and event
GPIO.setmode(GPIO.BCM)
button_pin = 17 # Connect button to pin 17 and gnd
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(button_pin, GPIO.FALLING, bouncetime=250)
while True:
try:
# Display the time every 1 second
if prev_time != str(datetime.now().strftime('%I:%M:%S %p')):
prev_time = str(datetime.now().strftime('%I:%M:%S %p'))
if counter == 5: os.system('clear')
if prev_counter <> counter:
print "Button Pressed"
prev_counter = counter
print "Counter: " + str(counter)
print str(datetime.now().strftime('%I:%M:%S %p'))
# Check for the button press event
if GPIO.event_detected(button_pin):
counter += 1
time.sleep(0.1)
except (KeyboardInterrupt, SystemExit):
os.system('clear')
GPIO.cleanup()
sys.exit(0)
答案 0 :(得分:0)