如何在python curses窗口中输出文本,其行数超过允许值?

时间:2015-03-16 10:27:01

标签: python python-curses

我想在我的python curses窗口中显示某些行作为输出,但是行数大于curses窗口中允许的总行数,因此我收到错误。

如何通过向下滚动到下一个屏幕来显示所有内容。我尝试了填充,但它不起作用。

这是我的代码

with open('input_csv.csv', 'rb') as f:
      reader = csv.reader(f)                # Create a reader object.
      row_num = 0
      screen.clear()
      screen.border(0)
      for row in reader:
          header = row
          col_num = 0
          screen.addstr(1+row_num, 5+30*col_num, header[col_num])
          screen.addstr(1+row_num, 10+30*(col_num+1), header[col_num+1], curses.A_BLINK)
          screen.addstr(3+row_num+1, 3, " ")
          screen.refresh()
          row_num += 2
      screen.getch()

1 个答案:

答案 0 :(得分:0)

你应该做一些像小菜单......

看看这个,你只需要编辑stringsmax_row(最大行数):

from __future__ import division  #You don't need this in Python3
import curses
from math import *



screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
screen.keypad(1)
curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_CYAN)
highlightText = curses.color_pair(1)
normalText = curses.A_NORMAL
screen.border(0)
curses.curs_set(0)
box = curses.newwin(12,64,1,1)
box.box()


strings = ["a","b","c","d","e","f","g","h","i","l","m","n"]
row_num = len(strings)
max_row = 10
pages = int(ceil(row_num/max_row))
position = 1
page = 1
for i in range(1,max_row + 1):
    if row_num == 0:
        box.addstr(1,1,"There aren't strings", highlightText)
    else:
        if (i == position):
            box.addstr(i,2,strings[i-1], highlightText)
        else:
            box.addstr(i,2,strings[i-1],normalText)
        if i == row_num:
            break

screen.refresh()


box.refresh()

x = screen.getch()
while x != 27:
    if (x == curses.KEY_DOWN):
        if page == 1:
            if position < i:
                position = position + 1
            else:
                if pages > 1:
                    page = page +1
                    position = 1 + (max_row * (page - 1))
        elif page == pages:
            if position < row_num:
                position = position + 1
        else:
            if position < max_row+(max_row*(page-1)):
                position = position + 1
            else:
                page = page + 1
                position = 1 + (max_row * (page - 1))
    if (x == curses.KEY_UP):
        if page == 1:
            if position > 1:
                position = position - 1
        else:
            if position > (1 + (max_row*(page-1))):
                position = position - 1
            else:
                page = page - 1
                position = max_row + (max_row * (page - 1))
    if (x == curses.KEY_LEFT):
        if page > 1:
            page = page - 1
            position = 1 + (max_row * (page - 1))

    if (x == curses.KEY_RIGHT):
        if page < pages:
            page = page + 1
            position = (1 + (max_row * (page - 1)))


    box.erase()
    screen.border(0)
    box.border(0)

    for i in range(1+(max_row*(page-1)),max_row+1+(max_row*(page-1))):
        if row_num == 0:
            box.addstr(1,1,"There aren't strings", highlightText)
        else:

            if (i+(max_row*(page-1)) == position+(max_row*(page-1))):
                box.addstr(i-(max_row*(page-1)),2,strings[i-1], highlightText)
            else:

                box.addstr(i-(max_row*(page-1)),2,strings[i-1],normalText)
            if i == row_num:
                break



    screen.refresh()
    box.refresh()
    x = screen.getch()

curses.endwin()
exit()