terminal - How to make a scrolling menu in python-curses -
there way make scrolling menu in python-curses? have list of records got query in sqlite3 , have show them in box more max number of rows: can make little menu show them without making curses crashing?
this code allows create little menu in box list of strings.
can use code getting list of strings sqlite query or csv file.
edit max number of rows of menu have edit max_row
.
if press enter program print selected string value , position.
from __future__ import division #you don't need in python3 import curses 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 ) max_row = 10 #max number of rows box = curses.newwin( max_row + 2, 64, 1, 1 ) box.box() strings = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "l", "m", "n" ] #list of strings row_num = len( strings ) pages = int( ceil( row_num / max_row ) ) position = 1 page = 1 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, str( ) + " - " + strings[ - 1 ], highlighttext ) else: box.addstr( i, 2, str( ) + " - " + strings[ - 1 ], normaltext ) if == 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 ) ) ) if x == ord( "\n" ) , row_num != 0: screen.erase() screen.border( 0 ) screen.addstr( 14, 3, "you have pressed '" + strings[ position - 1 ] + "' on position " + str( position ) ) box.erase() screen.border( 0 ) box.border( 0 ) 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 ( + ( max_row * ( page - 1 ) ) == position + ( max_row * ( page - 1 ) ) ): box.addstr( - ( max_row * ( page - 1 ) ), 2, str( ) + " - " + strings[ - 1 ], highlighttext ) else: box.addstr( - ( max_row * ( page - 1 ) ), 2, str( ) + " - " + strings[ - 1 ], normaltext ) if == row_num: break screen.refresh() box.refresh() x = screen.getch() curses.endwin() exit()
Comments
Post a Comment