简体   繁体   English

如何使用curses输入后内联打印

[英]How to print inline after input using curses

I want to receive input by prompt with the following structure:我想通过提示接收具有以下结构的输入:

-----------------------------
|                           |
| Digit your name:[]        |
|                           |
-----------------------------

where '[]' is the input cursor.其中“[]”是输入 cursor。

The code that i made (not working) with raw input is:我使用原始输入制作(不工作)的代码是:

print('-----------------------------------------------------------------------')
print('| {:67s} |'.format(''))
print('| {:67s} |'.format(input('Digit your name:')))
print('| {:67s} |'.format(''))
print('-----------------------------------------------------------------------')

How can I make it working with curses ?我怎样才能使它与curses一起工作? Thanks谢谢

If you want to do this in curses you need to have a main funtion where you put in the stdscr as an argument, like this:如果您想在 curses 中执行此操作,您需要有一个 main 函数,将 stdscr 作为参数放入其中,如下所示:

import curses


def main(stdscr):
    stdscr.getch()


curses.wrapper(main)

And the stdscr.getch() just waits for the user to press any key.而 stdscr.getch() 只是等待用户按下任意键。

Then you can print out a string on the text with the stdscr.addstr(0, 0, "Digit your name:") .然后,您可以使用stdscr.addstr(0, 0, "Digit your name:")在文本上打印出一个字符串。 The first two arguments are the y and x coordinates.前两个 arguments 是yx坐标。 so then it will look like this那么它看起来像这样

import curses


def main(stdscr):
    stdscr.addstr(0, 0, "Digit your name:")
    stdscr.getch()


curses.wrapper(main)

Then if you want a kind of input , you need curses.echo() , which will just echo your input.然后,如果您想要一种input ,则需要curses.echo() ,它只会回显您的输入。 And then stdscr.getstr(0, 0) .然后stdscr.getstr(0, 0) But those two arguments are where you want the user to write.但是这两个 arguments 是您希望用户写入的位置。 But if you run it:但是如果你运行它:

import curses


def main(stdscr):
    curses.echo()
    stdscr.addstr(0, 0, "Digit your name:")
    stdscr.getstr(0, len("Digit your name:"))


curses.wrapper(main)

You then need to say stdscr.addstr(0, len("Digit your name:") + 1)然后你需要说stdscr.addstr(0, len("Digit your name:") + 1)

And if you want to make a box here is a link.如果你想制作一个盒子,这里有一个链接。


I hope i could help you.我希望我能帮助你。 And remember.记住。 You can't do this in pycharm editor.您不能在 pycharm 编辑器中执行此操作。 You need to do it in the terminal您需要在终端中执行此操作

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

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