python - 在 stdscr 中解释 "ENTER"按键(Python 中的 curses 模块)

标签 python curses python-curses

我正在使用 Python 的 curses 模块。 在 stdscr 中,每当我按下回车键时,诅咒就会移动到同一行的第一列。我有几个问题。

  1. 这是什么原因?

  2. 有没有办法把诅咒移到下一行?

  3. 如果我想在按下 enter 键时做某些事情(执行某些功能或其他事情),那么在“如果”条件下会发生什么?例如

    if (condition which will determine if ENTER was pressed or not)
        # somecode
    

最佳答案

  1. What is the reason for that?

您需要调用 curses.noecho() 作为初始化的一部分。

  1. Is there a way to move the curse to the next line?

screen.move(y,x) 将移动到绝对位置。 screen.getyx() 会告诉您当前位置。

  1. If I want to do certain things (execute some function or something) on enter key press, then what will come in 'if' condition? e.g.

您认为您可以调用 getch() 并将结果与​​ KEY_ENTER 进行比较。实际上,您需要检查更多的值。根据您的终端设置、您使用的库和月相,您可能需要检查换行符(又名 \n、^J、ASCII 10)或回车符(\r, ^M, ASCII 13).

c = screen.getch()
if c == curses.KEY_ENTER or c == 10 or c == 13:
    # I hit ENTER

示例程序:

import curses

# Thanks, http://www.ipsum-generator.com
ipsum = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla
quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent
mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum
lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent
per conubia nostra, per inceptos himenaeos.'''

try:
    # Standard startup. Probably don't need to change this
    screen = curses.initscr()
    curses.cbreak()
    curses.noecho()
    screen.keypad(True)

    # Silly program to write to the screen,
    # wait for either <ENTER> or <Q>.
    # On <ENTER>, mess with the screen.
    # On <Q>, exit.
    screen.addstr(0, 0, ipsum)
    screen.move(0, 0)
    screen.refresh()
    i = 0
    j = 0

    while True:
        c = screen.getch()
        if c == ord('q'):
            exit(0)
        if c == curses.KEY_ENTER or c == 10 or c == 13:
            i += 1
            if i % 3 == 0:
                screen.addstr(0, 0, ipsum.lower())
            if i % 3 == 1:
                screen.addstr(0, 0, ipsum.upper())
            if i % 3 == 2:
                screen.addstr(0, 0, ipsum)
            screen.move(0, 0)
        if c == curses.KEY_DOWN:
            y, x = screen.getyx()
            maxy, maxx = screen.getmaxyx()
            screen.move((y+1) % maxy, x)
        screen.refresh()


finally:
    # Standard shutdown. Probably don't need to change this.
    curses.nocbreak()
    screen.keypad(0)
    curses.echo()
    curses.endwin()

引用:

关于python - 在 stdscr 中解释 "ENTER"按键(Python 中的 curses 模块),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32252733/

相关文章:

Python:两次调用 'curses' 会塞满我的终端

python - 如何在curses中启用鼠标移动事件

python - 无法在 PythonWin 中运行任何多处理脚本

python - 正则表达式 - 抓取带有句点 1.1、1.1.1 等的部分

python - 无法加载已保存的 FLANN 索引

python - 当 bkgd 字符为curses.ACS_CKBOARD 时,文本显示损坏

Python Curses - 模块 'curses' 没有属性 'LINES'

python - conda 错误 ssl 证书 : HTTPSConnectionPool(host=\'repo.anaconda.com\' , 端口=443

Python 诅咒 : Returning to the Previous Menu

c++ - 为什么 ncurses 小部件在单独的函数中创建时不能正常工作?