Python 调整大小时诅咒边框
Python Curses Border on Resize
我正在使用 curses 在 Python 2.7 中构建一个简单的可滚动菜单,并尝试使其针对任何 window 大小进行缩放,包括如果终端大小发生变化(即我希望它更大)或在我使用菜单时更小)。我有一个简单的测试代码,用来尝试找出这个问题。看来,如果这可能的话,我真的很接近,但我看到的是,当我调整我的终端 window 大小时(使用 mRemoteNG),边框画线填充 space,如图所示在屏幕截图的底部,在我垂直扩展 window 之后:
我用来测试的代码如下:
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()
放置方式,但它似乎永远无法摆脱诅咒边缘的线条残留 window。显然,如果这可能的话,我要么不知道将这些 one/both 放在哪里,要么我不在正确的轨道上。
在您的程序中,您应该检查 curses.KEY_RESIZE 作为 getch
的 return 值,在这种情况下,调用 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()
我正在使用 curses 在 Python 2.7 中构建一个简单的可滚动菜单,并尝试使其针对任何 window 大小进行缩放,包括如果终端大小发生变化(即我希望它更大)或在我使用菜单时更小)。我有一个简单的测试代码,用来尝试找出这个问题。看来,如果这可能的话,我真的很接近,但我看到的是,当我调整我的终端 window 大小时(使用 mRemoteNG),边框画线填充 space,如图所示在屏幕截图的底部,在我垂直扩展 window 之后:
我用来测试的代码如下:
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()
放置方式,但它似乎永远无法摆脱诅咒边缘的线条残留 window。显然,如果这可能的话,我要么不知道将这些 one/both 放在哪里,要么我不在正确的轨道上。
在您的程序中,您应该检查 curses.KEY_RESIZE 作为 getch
的 return 值,在这种情况下,调用 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()