在 Python 中将 Curses 矩形框扩展到终端的边缘

Extending Curses Rectangle Box to edge of Terminal in Python

我在使用 Python 在 Curses 中创建矩形时遇到了一些问题。

这是我的代码:

import curses
from curses.textpad import Textbox, rectangle

def draw_menu(stdscr):

    stdscr.clear()
    stdscr.refresh()

    while True:
        stdscr.refresh()


        writebox_uly = (int(stdscr.getbegyx()[0]))
        writebox_ulx = (int(stdscr.getbegyx()[1]))
        writebox_lry = (int(stdscr.getmaxyx()[0] * 0.7))
        writebox_lrx = (int(stdscr.getmaxyx()[1] - 1))

        rectangle(stdscr, writebox_uly, writebox_ulx, writebox_lry, writebox_lrx)

        editwin_uly = (int(stdscr.getmaxyx()[0] * 0.7 + 1))
        editwin_ulx = (int(stdscr.getbegyx()[1]))
        editwin_lry = (int(stdscr.getmaxyx()[0] - 1))
        editwin_lrx = (int(stdscr.getmaxyx()[1] - 2))

        rectangle(stdscr, editwin_uly, editwin_ulx, editwin_lry, editwin_lrx)

def main():
    curses.wrapper(draw_menu)

if __name__ == "__main__":
    main()

这将创建两个漂亮的矩形,如下图所示:

您可能会注意到下方框的右下角不是内嵌的。这是因为 editwin_lrx = (int(stdscr.getmaxyx()[1] - 2))

此行将右下 X 坐标(右下角)设置为终端的最大宽度减去 2

将其更改为 editwin_lrx = (int(stdscr.getmaxyx()[1] - 5)) 将使框更靠左。

因此,理论上,将其更改为 editwin_lrx = (int(stdscr.getmaxyx()[1] - 1)) 会将其稍微向右推。

但它不会崩溃。

Traceback (most recent call last):
  File "temp.py", line 32, in <module>
    main()
  File "temp.py", line 29, in main
    curses.wrapper(draw_menu)
  File "/usr/lib/python3.6/curses/__init__.py", line 94, in wrapper
    return func(stdscr, *args, **kwds)
  File "temp.py", line 26, in draw_menu
    rectangle(stdscr, editwin_uly, editwin_ulx, editwin_lry, editwin_lrx)
  File "/usr/lib/python3.6/curses/textpad.py", line 16, in rectangle
    win.addch(lry, lrx, curses.ACS_LRCORNER)
_curses.error: addch() returned ERR

是否可以将第二个框的右侧延伸到最边缘,使其与第一个框对齐?

您必须捕获异常并忽略它。 rectangle 函数如下所示:

def rectangle(win, uly, ulx, lry, lrx):                                         
    """Draw a rectangle with corners at the provided upper-left                 
    and lower-right coordinates.                                                
    """                                                                         
    win.vline(uly+1, ulx, curses.ACS_VLINE, lry - uly - 1)                      
    win.hline(uly, ulx+1, curses.ACS_HLINE, lrx - ulx - 1)                      
    win.hline(lry, ulx+1, curses.ACS_HLINE, lrx - ulx - 1)                      
    win.vline(uly+1, lrx, curses.ACS_VLINE, lry - uly - 1)                      
    win.addch(uly, ulx, curses.ACS_ULCORNER)                                    
    win.addch(uly, lrx, curses.ACS_URCORNER)                                    
    win.addch(lry, lrx, curses.ACS_LRCORNER)                                    
    win.addch(lry, ulx, curses.ACS_LLCORNER)

manual page 中所述,在写入低级时诅咒 returns 一个错误(rectangle 应该忽略)屏幕右上角:

  • At the bottom of the current scrolling region, and if scrollok is enabled, the scrolling region is scrolled up one line.

  • If scrollok is not enabled, writing a character at the lower right margin succeeds. However, an error is returned because it is not possible to wrap to a new line