What's the short way to add same symbol at the specified place in ncurses?

守給你的承諾、 提交于 2019-12-14 03:50:35

问题


I want to add str "#" in ncurse screen,with coordinate x(5 to 24), y(23 to 42) which is a square. But I can't figure out a simple way to do it.

I've tried:

stdscr.addstr(range(23,42),range(5,24),'#')

But this can't work. It needs 'integer'.

Does anyone can figure out an easy way to do this Job?

Thanks.


回答1:


First two arguments of addstr should be row, col as integer but your are passing list:

To make square to like this:

for x in range(23,42): # horizontal c 
  for y in range(5,24): # verticale r
    stdscr.addstr(y, x, '#')        

To fill colors, blinking, bold etc you can use attribute filed in function:

from curses import *
def main(stdscr):
    start_color()
    stdscr.clear()  # clear above line. 
    stdscr.addstr(0, 0, "Fig: SQUARE", A_UNDERLINE|A_BOLD)    
    init_pair(1, COLOR_RED, COLOR_WHITE)
    init_pair(2, COLOR_BLUE, COLOR_WHITE)
    pair = 1
    for x in range(3, 3 + 5): # horizontal c 
      for y in range(4, 4 + 5): # verticale r
        pair = 1 if pair == 2 else 2
        stdscr.addstr(y, x, '#', color_pair(pair))
    stdscr.addstr(11, 0, 'Press Key to exit: ')
    stdscr.refresh()
    stdscr.getkey()    
wrapper(main)

Output:

Old-answer:

Do like this for diagonal:

for c, r in zip(range(23,42), range(5,24)) :
  stdscr.addstr(c, r, '#')      

Code example to fill diagonal:

code x.py

from curses import wrapper
def main(stdscr):
    stdscr.clear()  # clear above line. 
    for r, c in zip(range(5,10),range(10, 20)) :
      stdscr.addstr(r, c, '#')  
    stdscr.addstr(11, 0, 'Press Key to exit: ')
    stdscr.refresh()
    stdscr.getkey()

wrapper(main)

run: python x.py, then you can see:

To make a Square do like:

from curses import wrapper
def main(stdscr):
    stdscr.clear()  # clear above line. 
    for r in range(5,10):
      for c in range(10, 20):
        stdscr.addstr(r, c, '#')        
    stdscr.addstr(11, 0, 'Press Key to exit: ')
    stdscr.refresh()
    stdscr.getkey()

wrapper(main)

Output:

PS: from your code it looks like you wants to fill diagonal, so I edited answer later for square.



来源:https://stackoverflow.com/questions/21677337/whats-the-short-way-to-add-same-symbol-at-the-specified-place-in-ncurses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!