What is a short way to add the same character to a specified location in ncurses?

I want to add str "#"in the ncursescreen coordinate x(5 to 24), y(23 to 42) which is a square. But I can’t understand an easy way to do this.

I tried:

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

But that will not work. He needs a "whole."

Can anyone find an easy way to do this?

Thank.

+3
source share
1 answer

The first two arguments addstrmust be string, col integers, but your list:

To make a square 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, blink, bold, etc., you can use the attribute registered in the 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:

output

Old answer:

Do it diagonally:

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

Example code for filling the 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, :

output

, :

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)

:

square

PS: , , .

+3

All Articles