Python Forum

Full Version: Curses script doesn't work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was bored and started to play with curses. I wrote a small script which just shows the key you hit and you are able to move that around. But it crashes. I don't know why.

It happens when I move the cursor at the bottom-right of the terminal emulator.

The system is Windows 10. But it's the same in the Windows Linux subsystem.

Here it is:
import curses

screen = curses.initscr()

curses.noecho()
curses.cbreak()
curses.curs_set(0)
screen.keypad(True)

y_pos, x_pos = 0, 0

height,width = screen.getmaxyx()  
midle = int(len(f"{height}x{width}") / 2)
screen.addstr(int(height / 2), int((width - midle) / 2), f"{height}x{width} Y={y_pos} X={x_pos}")
screen.refresh()

text = f""

try:
    while True:
        ch = screen.getch()
        
        if y_pos > height -1:
            y_pos = height -1
        if x_pos > width -1:
            x_pos = width -1
        
        if ch != curses.KEY_UP and ch != curses.KEY_DOWN and ch != curses.KEY_LEFT and ch != curses.KEY_RIGHT:
            text = f'{chr(ch)} {ch}'
            text.encode('utf-8')
        
        if ch == 27:
            curses.nocbreak()
            curses.echo()
            curses.curs_set(1)
            screen.keypad(False)
            print('Terminated by the user')
            break
            
        elif ch == curses.KEY_UP:
            if y_pos > 0:
                y_pos -= 1
                
        elif ch == curses.KEY_DOWN:
            if y_pos < height - 1:
                y_pos += 1
        
        elif ch == curses.KEY_LEFT:
            if x_pos > 0:
                x_pos -= 1
        
        elif ch == curses.KEY_RIGHT:
            if x_pos < width - len(text):
                x_pos += 1
        
        screen.erase()
        
        height,width = screen.getmaxyx()  
        midle = int(len(f"{height}x{width}") / 2)
        screen.addstr(int(height / 2), int((width - midle) / 2), f"{height}x{width} Y={y_pos} X={x_pos}")
        
        screen.addstr(y_pos, x_pos, text)
        screen.refresh()
        
except KeyboardInterrupt:
    curses.nocbreak()
    curses.echo()
    curses.curs_set(1)
    screen.keypad(False)
    print('Keyboard interrupt!')
except Exception as e:
    curses.nocbreak()
    curses.echo()
    curses.curs_set(1)
    screen.keypad(False)
    print('Something bad happened!')
    print(e)
Error:
Traceback (most recent call last): File "curses_ex.py", line 55, in <module> screen.addstr(y_pos, x_pos, text) _curses.error: addwstr() returned ERR
Alright, it seems that this behaviour is ok?!
Quote:Writing outside the window, subwindow, or pad raises curses.error. Attempting to write to the lower right corner of a window, subwindow, or pad will cause an exception to be raised after the string is printed.

I don't get it. Why is that? And how to solve it?
If I catch the exception the script just crashes again except I don't see the useless error message. And it ends with the shell prompt

except curses.error:
    pass