Python调整边框大小

时间:2017-01-19 18:42:41

标签: python curses

我在Python 2.7中使用curses构建一个简单的可滚动菜单,并尝试使其适用于任何窗口大小,包括终端大小是否发生变化(即我希望它在我使用菜单时更大或更小) 。我有一个简单的测试代码,我正在尝试找出这个问题。看来,如果这是可能的,我真的很接近,但我所看到的是,当我调整终端窗口的大小(使用mRemoteNG)时,边框绘制线条以填充空间,如截屏底部所示,我垂直地扩展了窗口:

resize-test_border-lines

我用来测试它的代码如下:

import curses
import os

VERSION = "0.1-dev" #version number

screen = curses.initscr() #initialize the curses window

#Configure color pairs for showing select menu options as highlighted
curses.start_color() #enable color for highlighting menu options
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) #color pair 1
highlightText = curses.color_pair(1) #color pair for highlighted menu option
normalText = curses.A_NORMAL #color pair for non-highlighted menu options

#Configure global variables for Curses
curses.noecho() #disable the keypress echo to prevent double input
curses.cbreak() #disable line buffers to run the keypress immediately
curses.curs_set(0)
screen.keypad(1) #enable keyboard use
screen.addstr(2, 2, "Screen Resize Test" + VERSION, curses.A_UNDERLINE)

#test screen resize
def main_screen():
    escape = False
    while escape == False:
        maxY, maxX = screen.getmaxyx()
        screen.border('|', '|', '-', '-', '+', '+', '+', '+')
        screen.addstr(4, 2, "MaxY: " + str(maxY))
        screen.addstr(5, 2, "MaxX: " + str(maxX))
        screen.refresh()

        x = screen.getch()

        if x == ord("q"):
            escape = True




main_screen()


curses.endwin() # *** CRITICAL *** this closes the curses menu and returns user to bash
os.system('clear') #clears the screen to avoid curses remnants

我尝试了screen.refresh()screen.clear()的许多不同的展示位置,但它似乎永远不会消除curses窗口边缘的线条残余。显然,如果这是可能的,我要么无法弄清楚放置其中一个/两个的位置,要么我不在正确的轨道上。

1 个答案:

答案 0 :(得分:1)

在你的程序中,你应该检查curses.KEY_RESIZE作为getch的返回值,在这种情况下,请调用screen.erase

此外,现有的screen.refresh来电是不必要的,因为screen.getch无论如何都会这样做。

这对我有用:

import curses
import os

VERSION = "0.1-dev" #version number

screen = curses.initscr() #initialize the curses window

#Configure color pairs for showing select menu options as highlighted
curses.start_color() #enable color for highlighting menu options
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) #color pair 1
highlightText = curses.color_pair(1) #color pair for highlighted menu option
normalText = curses.A_NORMAL #color pair for non-highlighted menu options

#Configure global variables for Curses
curses.noecho() #disable the keypress echo to prevent double input
curses.cbreak() #disable line buffers to run the keypress immediately
curses.curs_set(0)
screen.keypad(1) #enable keyboard use
screen.addstr(2, 2, "Screen Resize Test" + VERSION, curses.A_UNDERLINE)

#test screen resize
def main_screen():
    escape = False
    while escape == False:
        maxY, maxX = screen.getmaxyx()
        screen.border('|', '|', '-', '-', '+', '+', '+', '+')
        screen.addstr(4, 2, "MaxY: " + str(maxY))
        screen.addstr(5, 2, "MaxX: " + str(maxX))

        x = screen.getch()

        if x == ord("q"):
            escape = True
            curses.endwin()
        elif x == curses.KEY_RESIZE:
            screen.erase()
            screen.addstr(2, 2, "Screen Resize Test" + VERSION, curses.A_UNDERLINE)

main_screen()