0
這裏是我目前有上運行Raspbian傑西我樹莓派3代碼:如何在python中沒有按下按鍵之後執行操作?
#!/usr/bin/python
import time
import os
os.system('cls' if os.name == 'mt' else 'clear')
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.HIGH)
text = raw_input('Success! LED on. Press Enter to turn off...')
if text == "":
print('LED off.')
GPIO.output(18,GPIO.LOW)
else:
time.sleep(10)
os.system('cls' if os.name == 'mt' else 'clear')
print('Auto turn off in')
time.sleep(1)
print('5')
time.sleep(1)
print('4')
time.sleep(1)
print('3')
time.sleep(1)
print('2')
time.sleep(1)
print('1')
time.sleep(1)
print('LED off.')
GPIO.output(18,GPIO.LOW)
什麼它目前所做的是,如果你按另一個鍵然後進入,它會觸發第二個代碼序列,但我需要它,這樣當一個鍵沒有被按下10秒鐘時,第二個代碼序列就會運行,當你按下Enter鍵時,第一個代碼序列被運行。那可能嗎?
'raw_input'塊程序,所以你必須在線程中運行它 - 或者找到在互聯網功能'的getchar()'/'GET_CHAR()'它檢查鍵盤,但不會阻止它,然後你可以循環檢查鍵盤和時間。 – furas
你是什麼意思序列? – thesonyman101
@ thesonyman101第一個序列是「if」,第二個序列是「else」。 –