你會想看看curses
。在Google上搜索教程,例如here。 這是一個不錯庫創建一個界面,您在此處描述。
當你擁有簡約的界面運行,使用subprocess
-module啓動腳本,例如
import subprocess
subprocess.call(["my_script.sh","parameter1","parameter2"])
編輯
好像罵人不支持可選的名單,但ncurses的一樣。在Debian的倉庫,有python-urwid
,一些Python的綁定ncurses的。
一個例子是:
import urwid
import subprocess
games = {"Game1":"script_name1.sh",
"Game2":"script_name2.sh",
"Game3":"script_name3.sh",
"Game4":"script_name4.sh",
"Game5":"script_name5.sh",}
class ItemWidget (urwid.WidgetWrap):
def __init__ (self, id, description):
self.id = id
self.content = description
self.item = [
('fixed', 15, urwid.Padding(urwid.AttrWrap(
urwid.Text('%s:' % str(id)), 'body', 'focus'), left=2)),
urwid.AttrWrap(urwid.Text('%s' % description), 'body', 'focus'),
]
w = urwid.Columns(self.item)
self.__super.__init__(w)
def selectable (self):
return True
def keypress(self, size, key):
return key
def main():
palette = [
('body','dark cyan', '', 'standout'),
('focus','dark red', '', 'standout'),
('head','light red', 'black'),
]
def keystroke (input):
if input in ('q', 'Q'):
raise urwid.ExitMainLoop()
if input is 'enter':
view.set_header(urwid.AttrWrap(urwid.Text("selected"), 'head'))
focus = listbox.get_focus()[0].content
try:
subprocess.call(games[focus])
except OSError, ose:
view.set_header(urwid.AttrWrap(urwid.Text(
'error starting %s: %s' % (str(focus), str(ose))), 'head'))
items = []
game_keys = sorted(games.keys())
for i, k in enumerate(game_keys):
items.append(ItemWidget(i, k))
header = urwid.AttrMap(urwid.Text('selected:'), 'head')
listbox = urwid.ListBox(urwid.SimpleListWalker(items))
view = urwid.Frame(urwid.AttrWrap(listbox, 'body'), header=header)
loop = urwid.MainLoop(view, palette, unhandled_input=keystroke)
loop.run()
if __name__ == '__main__':
main()
這是從nicosphere適配。