使用Python,我试图使用addstr()将光标位置写入到curses窗口的右下角,但出现错误。 ScreenH-2可以正常工作,但是从窗口底部的第二行开始打印。 ScreenH-1根本不起作用。我究竟做错了什么?

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

最佳答案

您需要像从高度中一样从宽度中减去1。否则,字符串将超过屏幕的宽度。

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

关于python - 为什么我无法在python curses窗口中将addstr()添加到最后一行/col?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22456527/

10-12 22:23