使用PyInquirer

时间:2019-12-22 10:26:14

标签: python-3.x windows terminal

我想创建一个使用用户时间输入的脚本。目前,我正在使用PyInquirer,因为它看起来很酷,但是我不确定它是否能够满足我的需求。

第一个提示要求用户选择“持续时间”或“时间”,如下所示: enter image description here

如果用户然后选择“时间”,则下一个提示如下: enter image description here

此处,输入必须仅具有HH:MM [AM / PM]的默认格式,其中只能使用向上和向下箭头选择或键入值。光标必须忽略并跳过“:”。

因此,可以使用向上和向下箭头键键入或旋转HH和MM,也可以旋转AM / PM。如果键入HH和MM,则光标必须自动跳过冒号。

到目前为止,这是我的代码:

from PyInquirer import prompt, print_json

typeQ = [
    {
        'type' : 'list',
        'name' : 'type',
        'message' : 'Duration or Time based?',
        'choices' : [
            'Duration',
            'Time'
        ]
    }
]

time = [
    {
        'type' : 'input',
        'name' : 'time',
        'message' : 'Specify time:'
    }
]

typeA = prompt(typeQ)
if (typeA == 'Duration'):
    pass
else:
    time = prompt(time)

1 个答案:

答案 0 :(得分:0)

这是一个示例,如何在while循环中将带箭头的python curses库用作键输入。 mytime()方法只能通过箭头按键访问。还添加了一些其他键,以便您可以使用它。有关内嵌评论,请参见“ ”。

请下次在问题显示的屏幕后面张贴您的示例代码以及您尝试过的拳头。它的航行有点盲目,没有代码。如果包括在内,将使您更容易预期使用的结构。它也有助于提高您的问题的投票力,而不是由于所谓的“缺乏努力”而使人们降低其投票率。

现在,您已经拥有我发布的内容,并且可能需要对其进行改装以适合您自己的代码。这是一次有趣的晨练。享受。

import curses, sys, time, re

screen = curses.initscr()

curses.curs_set(0)
screen.keypad(True)
curses.noecho()

regex_time = re.compile('[0-9][0-9][:][0-9][0-9]')  # time expression selection key.

def mytime():

    screen.addstr("\nTime entry please + [enter]. Input format is [HH:mm].\n")

    event = screen.getstr(5)  # use (0, 0, 15) for location of input string top left in cmd window.

    if regex_time.match(event.decode()):
        screen.addstr("\n You entry = %s h.\n\n >> Add code to do something else here << \n\n" % event.decode())
        screen.refresh()
        time.sleep(2)

        # do something else goes here.

    else:
        screen.addstr('\nDid not receive time\nTry again next round.\n')
        screen.refresh()
        time.sleep(2)

        # do something else goes here.


while True:

    try:
        screen.addstr("Press a key\n")
        curses.echo()
        event = screen.getch()  # get single character

        screen.refresh()

        if event == curses.KEY_LEFT:
            screen.addstr("Left Arrow Key pressed\n")
            screen.refresh()
            time.sleep(0.5)

            mytime()

        elif event == curses.KEY_RIGHT:
            screen.addstr("Right Arrow Key pressed\n")
            screen.refresh()
            time.sleep(2)

        elif event == 81:  # 'Q' for quite program.

            curses.echo() # shows which char you pressed.
            screen.addstr('\nThe Quite key "%s" was pressed\n' % (event))
            screen.refresh()
            time.sleep(3)
            break

        else:
            # keystroke identification except for above and regular expression that are comform time annotation.

            curses.echo() # shows which char you pressed.
            screen.addstr('\nThe key "%s" was pressed\n' % (event))
            time.sleep(2)
            screen.refresh()

    except Exception as err:
#        continue
        print (err)

# below code could replace the break in the quite elif statement.
screen.addstr("\nExit program.\n")
screen.refresh()
time.sleep(3)
curses.endwin()