简体   繁体   English

Python 多线输入

[英]Python multi-line input

Is there any way to have multi-line input in Python?有没有办法在Python中进行多行输入? Solutions I have seen don't allow you to edit past lines after you have pressed enter and have other problems.我看到的解决方案不允许您在按下回车键后编辑过去的行并遇到其他问题。 I am making a simple text editor in Python and I want to have a text input that can be multiline and pressing buttons like left or right would move the text cursor. I need it to stop taking input when certain buttons are pressed, like ctrl-s or ctrl-q.我正在 Python 中制作一个简单的文本编辑器,我想要一个可以是多行的文本输入,按下向左或向右等按钮将移动文本 cursor。我需要它在按下某些按钮时停止输入,例如 ctrl- s 或 ctrl-q。 So it would be nice if there was a way to get text input following these rules that would specify which button had been pressed to end the input when it was ended.因此,如果有一种方法可以按照这些规则获取文本输入,这些规则将指定在输入结束时按下哪个按钮来结束输入,那就太好了。 For example, a function that would return a tuple with the button pressed and the text.例如,一个 function 将返回一个包含按下的按钮和文本的元组。
I am doing this from the console.我正在从控制台执行此操作。

You might want to take a look at the curses module that allows you to control various aspects of console input and output. 您可能需要看一看curses模块,该模块可让您控制控制台输入和输出的各个方面。 Start with the official HOWTO: http://docs.python.org/howto/curses.html 从官方的HOWTO开始: http : //docs.python.org/howto/curses.html

import sys
print("Message? (Ctrl+D to end)")
msg = sys.stdin.readlines()
print(msg)

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

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