在ncurses的指定位置添加相同符号的简短方法是什么? [英] What's the short way to add same symbol at the specified place in ncurses?

查看:73
本文介绍了在ncurses的指定位置添加相同符号的简短方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ncurse屏幕上添加坐标x(5 to 24)y(23 to 42)的str "#" 这是一个正方形.但是我想不出一种简单的方法.

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.

我尝试过:

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?

谢谢.

推荐答案

addstr的前两个参数应为row,col为整数,但您正在传递列表:

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

使正方形像这样:

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)

输出:

旧答案:

对角线这样:

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

填充对角线的代码示例:

Code example to fill diagonal:

代码 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)

运行:python x.py,那么您可以看到:

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)

输出:

PS:从您的代码看来,您似乎想填充对角线,因此我稍后编辑了正方形的答案.

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

这篇关于在ncurses的指定位置添加相同符号的简短方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆