Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I addstr() to last row/col in python curses window?

Tags:

python

curses

Using Python, I'm trying to write the cursor location to the lower right corner of my curses window using addstr() but I get an error. ScreenH-2 works fine but is printed on the 2nd line up from the bottom of the winddow. ScreenH-1 does not work at all. What am I doing wrong?

import curses

ScreenH = 0
ScreenW = 0
CursorX = 1
CursorY = 1

def repaint(screen):   
   global ScreenH
   global ScreenW
   global CursorX
   global CursorY

   ScreenH, ScreenW = screen.getmaxyx()
   cloc = '   ' + str(CursorX) + ':' + str(CursorY) + ' '
   cloclen =  len (cloc)
   screen.addstr (ScreenH - 1, ScreenW - cloclen, cloc,  curses.color_pair(1));


def Main(screen):
   curses.init_pair (1, curses.COLOR_WHITE, curses.COLOR_BLUE)
   repaint (screen)   

   while True:
      ch = screen.getch()
      if ch == ord('q'):
         break

      repaint (screen)     


curses.wrapper(Main)

  File "test.py", line 17, in repaint
    screen.addstr (ScreenH - 1, ScreenW - cloclen, cloc,  curses.color_pair(1));
_curses.error: addstr() returned ERR
like image 611
wufoo Avatar asked Mar 17 '14 14:03

wufoo


People also ask

How do you clear the screen curse in Python?

To clear characters until the end of the line, use clrtoeol(), To clear characters until the end of the window, use clrtobot().

What is Python curses?

What is curses? ¶ The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals; such terminals include VT100s, the Linux console, and the simulated terminal provided by various programs.

What is curses module?

The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling. While curses is most widely used in the Unix environment, versions are available for Windows, DOS, and possibly other systems as well.


2 Answers

You could also use insstr instead of addstr:

screen.insstr(ScreenH - 1, ScreenW - 1 - cloclen, cloc,  curses.color_pair(1))

That will prevent the scroll, and thus allow you to print up to the very last char in last line

like image 119
MestreLion Avatar answered Oct 13 '22 00:10

MestreLion


You need to substract 1 from the width as you did for the height. Otherwise the string will exceed the width of the screen.

screen.addstr(ScreenH - 1, ScreenW - 1 - cloclen, cloc,  curses.color_pair(1))
                                   ^^^
like image 23
falsetru Avatar answered Oct 12 '22 23:10

falsetru