我正在尝试使用Python进入Curses,而且我对这两个主题都相当新。我正在尝试制作一个可滚动的菜单对象,到目前为止它看起来不错,但我遇到的问题是当滚动浏览一些测试数据时,窗口边框消失了。我已经在代码中的几个点尝试了window.clear()和window.refresh(),但是我没有做任何事情使得边框持久。
#!/usr/bin/python
import curses
import curses.panel
class CursesMenu():
pos = 0
c = None
menuItems = None
selection = None
dimensions = None
MAX_DISPLAYED = 5
displayList = list(range(MAX_DISPLAYED))
subwin_key_up = None
subwin_key_down = None
def __init__(self, menuItems=None):
self.menuItems = menuItems
curses.wrapper(self.main_menu, menuItems)
def scrollDisplayUp(self): #shift each element of displayList down one
for i in range(len(self.displayList)):
self.displayList[i] -= 1
def scrollDisplayDown(self): #shift each element of displayList up one
for i in range(len(self.displayList)):
self.displayList[i] += 1
def posUp(self, win): # Lower pos = higher placement
if self.pos == 0: #loop back around
self.pos = len(self.menuItems) - 1
self.displayList = list(range(len(self.menuItems) - self.MAX_DISPLAYED, len(self.menuItems)))
win.clear()
else:
self.pos -= 1
def posDown(self, win): # Higher pos = lower placement
if self.pos >= len(self.menuItems) - 1: #loop back around
self.pos = 0
self.displayList = list(range(self.MAX_DISPLAYED))
win.clear()
else:
self.pos += 1
def main_menu(self, win, menuItems=None):
self.dimensions = win.getmaxyx()
curses.curs_set(0) #invisible cursor
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
highlighted = curses.color_pair(1)
normal = curses.A_NORMAL
sub = win.subwin(self.MAX_DISPLAYED + 2, 20, 1, 1)
sub.immedok(True) #jet fuel can't melt steel beams
sub.box()
while self.c != ord('\n'): #run until 'enter' gets pressed
sub.box()
self.dimensions = win.getmaxyx()
if self.c == curses.KEY_UP:
self.posUp(sub)
if self.pos <= self.displayList[0] - 1:
self.scrollDisplayUp()
sub.clear()
sub.refresh()
if self.c == curses.KEY_DOWN:
self.posDown(sub)
if self.pos >= self.displayList[-1] + 1:
self.scrollDisplayDown()
sub.clear()
sub.refresh()
for i, j in zip(self.displayList, range(self.MAX_DISPLAYED)):
if i != self.pos:
sub.addstr(j+1, 1, menuItems[i], normal)
else:
sub.addstr(j+1, 1, menuItems[i], highlighted)
self.c = win.getch()
self.selection = menuItems[self.pos]
这是我用来测试它的一个小脚本:
#!/usr/bin/python
import cursesmenu
menulist=['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel', \
'India', 'Juliett', 'Kilo', 'Lima', 'Mike', 'November', 'Oscar', 'Papa', 'Quebec', \
'Romeo', 'Sierra', 'Tango', 'Uniform', 'Victor', 'Whiskey', 'Xray', 'Yankee', 'Zulu']
m = cursesmenu.CursesMenu(menulist)
print m.selection
print m.dimensions
答案 0 :(得分:0)
通过阅读脚本至少看到两个问题:
sub
是绘制框的位置,它是win
的子项。您正在阅读win
,其中refresh
(覆盖sub
)。请参阅manual page。sub
,这也会删除该框。答案 1 :(得分:-3)
感谢Anand,解决了消失的边界,但后来我发现陈旧的文字粘在一起。我通过将sub.border(0)和sub.refresh()放回sub.clear()来解决问题。