【问题标题】:Why can't I addstr() to last row/col in python curses window?为什么我不能在 python 诅咒窗口中将str()添加到最后一行/列?
【发布时间】:2014-04-22 18:51:36
【问题描述】:

使用 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

【问题讨论】:

    标签: python curses


    【解决方案1】:

    您也可以使用insstr 代替addstr

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

    这将阻止滚动,从而允许您打印到最后一行的最后一个字符

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 好的,很好。但是,如果我将 addstr() 行简化为 screen.addstr (ScreenH - 1, ScreenW - 1, '*', curses.color_pair(1)) 为什么它仍然会出错?我也是这样从宽度和高度中减去 1 的。就像我无法在左下角字符上打印一样。
      • @wufoo,因为它会导致滚动被禁用,除非你调用screen.scrollok(1)
      • 啊,是的。感谢您的澄清!
      猜你喜欢
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2020-03-25
      • 2013-06-10
      • 2011-02-04
      • 1970-01-01
      相关资源
      最近更新 更多