Python中断不使用os.system(' clear')

时间:2015-03-08 22:15:01

标签: python raspberry-pi interrupt

我在我的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)

1 个答案:

答案 0 :(得分:0)

可以是子进程的signals;甚至复制文件描述符会干扰事件机制。

您无需执行clear命令来清除屏幕 - 您只需输出ANSI转义码'\033[2J'即可:

print '\033[2J'