python软件菜单如何设计_如何在python-curses中制作滚动菜单

该代码示例展示了如何在Python-curses环境下创建一个滚动菜单,用于显示从SQLite查询或CSV文件中获取的字符串列表。通过上下左右键操作,用户可以在有限的屏幕空间内浏览超过最大显示行数的记录,而不会导致终端崩溃。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

There is a way to make a scrolling menu in python-curses?

I have a list of records that I got from a query in sqlite3 and I have to show them in a box but they are more than the max number of rows: can I make a little menu to show them all without making curses crashing?

解决方案

This code allows you to create a little menu in a box from a list of strings.

You can also use this code getting the list of strings from a sqlite query or from a csv file.

To edit the max number of rows of the menu you just have to edit max_row.

If you press enter the program will print the selected string value and its position.

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 )

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

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, str( i ) + " - " + strings[ i - 1 ], highlightText )

else:

box.addstr( i, 2, str( i ) + " - " + 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 ) ) )

if x == ord( "\n" ) and 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 )

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, str( i ) + " - " + strings[ i - 1 ], highlightText )

else:

box.addstr( i - ( max_row * ( page - 1 ) ), 2, str( i ) + " - " + strings[ i - 1 ], normalText )

if i == row_num:

break

screen.refresh()

box.refresh()

x = screen.getch()

curses.endwin()

exit()

R3M6Z.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值