Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Curses script doesn't work
#1
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#2
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to be able to scroll up in curses to see previous text. Caiden 1 741 Jul-28-2023, 01:15 PM
Last Post: deanhystad
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 928 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,772 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 882 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,704 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,179 May-30-2022, 03:31 PM
Last Post: bowlofred
  Make my py script work only on 1 compter tomtom 14 3,816 Feb-20-2022, 06:19 PM
Last Post: DPaul
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,518 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,005 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,695 Oct-30-2021, 11:20 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020