dammerel
Posts: 1
Joined: Mon Sep 16, 2019 9:13 am

Error using "curses" to control RPI zero

Mon Sep 16, 2019 9:29 am

Hi All First post here

** This is the code I have tried to used (from Explaining Computers)
------

Code: Select all

# import curses
import curses

# Get the curses window, turn off echoing of keyboard to screen, turn on
# instant (no waiting) key response, and use special values for cursor keys
screen = curses.initscr()
curses.noecho() 
curses.cbreak()
screen.keypad(True)

try:
        while True:   
            char = screen.getch()
            if char == ord('q'):
                break
            elif char == curses.KEY_UP:
                print "up"
            elif char == curses.KEY_DOWN:
                print "down"
            elif char == curses.KEY_RIGHT:
                print "right"
            elif char == curses.KEY_LEFT:
                print "left"
            elif char == 10:
                print "stop"    
             
finally:
    #Close down curses properly, inc turn echo back on!
    curses.nocbreak(); screen.keypad(0); curses.echo()
    curses.endwin()
----
** First issue was:
---
Python 3.7.3 (/usr/bin/python3)
>>> %Run 'curses test.py'
Traceback (most recent call last):
  File "/home/pi/curses test.py", line 17
    print "up"
             ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("up")?
----

** Which I fixed with extra (  ) but the second issue was :
--
>>> %Run 'curses test.py'
Traceback (most recent call last):
  File "/home/pi/curses test.py", line 6, in <module>
    screen = curses.initscr()
  File "/usr/lib/python3.7/curses/__init__.py", line 30, in initscr
--
** Which I'm unsure of, I'm using a RPI zero and Thonny IDE

Thanks Andrew

[moderator added code tags]

Andyroo

Re: Error using "curses" to control RPI zero

Mon Sep 16, 2019 12:13 pm

Welcome to the forum,

Curses is designed to be used in a terminal / condole session and not a GUI.

The print problem is due to you mixing up Python 2 (print without brackets) and Python 3. This is normal if you are new to the Pi and copying old web sites. If you are using Buster and Thonny as the editor, it will default to Python 3.

Tu run your program
1) Start the terminal
2) Enter

Code: Select all

python3 programname.py
assuming you have saved it in the home directory :lol:

A couple of minor points that will save hassle on-going

A) Try to include a line to the original article (rather than videos) - folk can then see what was supposed to happen
B) Use the code blocks (5th button </>) to surround your code as this keeps indentation correct - vital in Python as you will see.

Return to “General discussion”