简体   繁体   English

Python 运行简单 shell 脚本的文本菜单

[英]Python text menu that runs simple shell script

I have no idea how to go about this so I'm not sure where to start.我不知道如何 go 关于这个所以我不知道从哪里开始。

Basically I want to write a python script that will present a user with a list of items that can be toggled through and selected using the arrow keys and enter key.基本上我想编写一个 python 脚本,它将向用户显示一个项目列表,可以使用箭头键和回车键切换和选择这些项目。 If python isn't able to do this is there another language that can?如果 python 不能做到这一点,是否有另一种语言可以做到?

The selection made will then run a simple line of shell script (in raspbian)所做的选择将运行一行简单的 shell 脚本(在 raspbian 中)

The idea is that I'll use this a list of games that my raspberry pi will run using MAME.我的想法是,我将使用我的树莓派将使用 MAME 运行的游戏列表。 ie a nice simple way of selecting a game as opposed to writing in the line of code every time.即选择游戏的一种很好的简单方法,而不是每次都在代码行中编写。

Cheers干杯

You'd want to look at curses . 你想看看curses Search Google for tutorials, eg, here . 在Google上搜索教程,例如, 在这里 It's a nice library to create an interface as you describe here. 如您在此处所述,创建界面是一个很好的库。

When you have your minimalistic GUI running, use the subprocess -module to launch your scripts, eg 当您运行简约GUI时,使用subprocess进程 - 模块来启动脚本,例如

import subprocess
subprocess.call(["my_script.sh","parameter1","parameter2"])

Edit 编辑

Seems like curses doesn't support selectable lists, but ncurses does. 好像curses不支持可选列表,但ncurses确实如此。 In the Debian repositories, there is python-urwid , some Python-bindings to ncurses. 在Debian存储库中,有一些python-urwid ,一些与ncurses绑定的Python。

An example would be: 一个例子是:

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()

This is adapted from nicosphere . 这是改编自nicosphere

You need to change "," to "as" at line 45 to put it to work at python 3.10:您需要在第 45 行将“,”更改为“as”以使其在 python 3.10 工作:

        except OSError as ose:
            view.set_header(urwid.AttrWrap(urwid.Text(
                'error starting %s: %s' % (str(focus), str(ose))), 'head'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM