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
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 530 Jul-17-2021, 04:19 PM
Last Post: bill_z
  string function doesn't work in script ClockPillow 3 263 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  curses issue otalado 2 443 Jun-29-2021, 02:07 PM
Last Post: tmz
Photo Windows 10 PowerShell doesn't work Amy 3 1,014 Apr-27-2021, 01:33 PM
Last Post: jefsummers
  Why doesn't this print statement work? stylingpat 10 1,003 Mar-23-2021, 07:54 PM
Last Post: buran
  keyboard module doesn't work in the microsoft version terminal of python. username 1 519 Feb-25-2021, 05:19 PM
Last Post: Larz60+
  How to make curses.border() use A_BOLD atttribute? pjfarley3 0 381 Feb-03-2021, 11:22 PM
Last Post: pjfarley3
  pygame music doesn't work CompleteNewb 1 1,555 Jan-30-2021, 09:25 AM
Last Post: Tyrel
  My first script does not work anymore redfirebrooks 2 680 Jan-05-2021, 10:49 AM
Last Post: redfirebrooks
  My code doesn't work, can someone help me? aldasrasickas 5 698 Dec-21-2020, 02:26 PM
Last Post: aldasrasickas

Forum Jump:

User Panel Messages

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