簡體   English   中英

如何在Python Raspberry PI中為GPIO命令分配鍵盤鍵

[英]How to assign keyboard keys to a GPIO command in Python Raspberry PI

我正在嘗試使用Python制作Raspberry Pi機器人,但我真的不知道如何使用curses為運動分配鍵。 我對此很陌生,希望獲得幫助。

我在下面的代碼中寫了我的嘗試:

import RPi.GPIO as GPIO
import curses 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
try: 
    stdscr = curses.initscr()
    curses.noecho()
    curses.cbreak()
    stdscr.keypad(1)
    while 1:
        c = stdscr.getch()
        if c == ord('p'):
             stdscr.addstr("")
        elif c == 37:
            GPIO.output(7,False)
            GPIO.output(11,True)
            GPIO.output(13,False)
            GPIO.output(13,True)

finally: 
    curses.nocbreak(); stdscr.keypad(0)l curses.echo
    curses.endwin()
    GPIO.cleanup()

我不斷收到以下錯誤:

Traceback (most recent call last):
File "/home/pi/ROBOT_FINAL.py", line 26, in <module>
curses.nocbreak(); stdscr.keypad(0); curses.echo()
error: must call initscr() first

請幫忙

謝謝,雅利安

查看您的代碼,似乎在try block內部發生錯誤。 不幸的是,目前我只能假設該錯誤發生在curses.initscr()調用期間。 即使發生錯誤,您的finally block也始終會執行。

要進一步診斷,請執行以下操作:是否可以在try block添加一個except並打印錯誤?

如果看到類似AttributeError: 'Module' object has no attribute 'initscr'的錯誤AttributeError: 'Module' object has no attribute 'initscr' ,則可能是將python腳本命名為curses 重命名它並刪除“ curses.pyc”文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM