在基于 Python 文本的 GUI (TUI) 中输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18177142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:05:42  来源:igfitidea点击:

Input in a Python text-based GUI (TUI)

pythonuser-interface

提问by Cold Diamondz

I have been trying to make my own UI that is text-based in python 2.7, but I thought of an idea about input. So I figured something like this: input = raw_input("|" + "input: ".center(78) + "|"), but of coarse, the cursor is way far on the right (just realized it wouldn't work before i even typed it in :P). So, the question is, is how do I put an input in the middle of the screen with text on the same line (on both sides) and have the cursor type after I write "Input: "? And if your wondering, im using this:

我一直在尝试在 python 2.7 中制作我自己的基于文本的 UI,但我想到了一个关于输入的想法。所以我想到了这样的事情:input = raw_input("|" + "input: ".center(78) + "|")但是粗略地说,光标在右边很远(只是在我输入它之前意识到它不起作用:P)。所以,问题是,我如何在屏幕中间放置一个输入,文本在同一行(两侧)并在我写下“输入:”后有光标类型?如果你想知道,我正在使用这个:

if True:
    print c + "Hi! This is a text-based GUI!".center(78, h) + c
    print c + "-" * 78 + c
    print v + "OPTIONS".center(78) + v
    print c + "-" * 78 + c
    print v + "1 - Quit".center(78) + v
    for i in range(1, 7):
        print v + " " * 78 + v
    print c + "-" * 78 + c

in the interpreter and it looks decent:

在解释器中,它看起来不错:

+------------------------Hi! This is a text-based GUI!-------------------------+
+------------------------------------------------------------------------------+
|                                   OPTIONS                                    |
+------------------------------------------------------------------------------+
|                                   1 - Quit                                   |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+

Note: I use Windows XP

注意:我使用的是 Windows XP

采纳答案by rook

Also try useful built atop curseshigh level framework urwid. With that thing you could do rich and colorful interfaces. Buttons, edit fields, even status barsand progress barsand all that you need. To start working you only need Python cursesinstalled and urwidfolder with its sources (you can transfer whole urwidlibrary with your application as standalone bundle!). It works even under cygwinunder Windows XP/7 where is, as we know, no cursesports for Python.

还可以尝试有用的构建在curses高级框架之上urwid。有了那个东西,你可以做丰富多彩的界面。按钮编辑字段、甚至状态栏进度条以及您需要的一切。要开始工作,您只需要curses安装Python和urwid文件夹及其源(您可以将整个urwid库与您的应用程序作为独立包一起传输!)。它的工作原理,即使在cygwin在Windows XP / 7这里,是因为我们知道,没有curses了港口Python

urwidportfolio

urwid文件夹

No more lowlevel, sometimes very boring curses:)

没有更多的低级,有时很无聊curses:)

回答by Martijn Pieters

The input()function is not nearly sophisticated for this kind of task.

input()对于此类任务,该功能并不复杂。

You'd be better of with a library that can control the Unix terminal, such as the curseslibrary. The library essentially lets you build a simple terminal GUI.

您最好使用可以控制 Unix 终端的curses,例如library。该库本质上允许您构建一个简单的终端 GUI。

If you need more, take a look at Urwidas well. Urwid offers more complex GUI widgets for the discerning terminal GUI developer.

如果您需要更多,请查看Urwid。Urwid 为挑剔的终端 GUI 开发人员提供了更复杂的 GUI 小部件。

You can't use the cursesmodule on Windows unfortunately; apparently there are DOS and OS/2 ports but it is primarily a POSIX-only library. On Windows you'll have to use a port such as wcurses, or you could try the consolemodule(the latter drives the Windows CMD console in a similar manner).

curses不幸的是,您不能在 Windows 上使用该模块;显然有 DOS 和 OS/2 端口,但它主要是一个仅限 POSIX 的库。在 Windows 上,您必须使用诸如 的端口wcurses,或者您可以尝试使用该console模块(后者以类似方式驱动 Windows CMD 控制台)。

回答by seanmcl

For a good text-based ui, you could use curses.

对于一个好的基于文本的用户界面,你可以使用curses。

http://docs.python.org/2/library/curses.html

http://docs.python.org/2/library/curses.html

回答by Cold Diamondz

Well, all of your answers were great, but I think I can use this for inputs instead of a 3rd party library:

好吧,您的所有答案都很棒,但我认为我可以将其用于输入而不是 3rd 方库:

c = "+"
h = "-"
print c + h*78 + c
inputa = raw_input(" "*32 + "Input example: ")
print c + h*78 + c

which would work for me.

这对我有用。

回答by Peter Brittain

What you need for this sort of text UI is a terminal library that understands the basic layout and capabilities of your screen and the supported input devices.

这种文本 UI 需要一个终端库,它了解屏幕的基本布局和功能以及支持的输入设备。

On Linux (or OSX), the widely recognised standard is ncurses. Python provides a moduleto wrap this native library. However, this (and any package that uses this - e.g. urwid) are of limited use on Windows.

在 Linux(或 OSX)上,广泛认可的标准是ncurses. Python 提供了一个模块来包装这个本机库。然而,这个(以及任何使用它的包 - 例如urwid)在 Windows 上的使用是有限的。

In your case, you need to use something else that provides access to the native Win32 console API. That would either be cygwin, a custom install of PDcurses, or a package like pywin32.

在您的情况下,您需要使用其他东西来提供对本机 Win32 控制台 API 的访问。那可能是 cygwin、PDcurses 的自定义安装,或者像pywin32.

Alternatively, if you really don't want to worry about all that OS specific nonsense, you could just install asciimatics. This provides a cross-platform APIto place text anywhere on the screen and process keyboard input. In addition, it provides higher level widgetsto create text UIs like this:

或者,如果您真的不想担心所有特定于操作系统的废话,您可以安装asciimatics。这提供了一个跨平台的 API来将文本放置在屏幕上的任何位置并处理键盘输入。此外,它还提供了更高级别的小部件来创建这样的文本 UI:

Text UI widgets

文本 UI 小部件

Full disclosure: Yes - I am the author of this package.

完全披露:是的 - 我是这个包的作者。

回答by icarus74

Adding another option few years after original question. As per the author of Picotui:

在原始问题几年后添加另一个选项。根据Picotui的作者:

Picotui is a Text User Interface (TUI) widget library for Python3. It is known to work with CPython3 and Pycopy (Unix version is officially supported for the latter), but should work with any Python3 implementation which allows to access stdin/stdout file descriptors.

Picotui 是 Python3 的文本用户界面 (TUI) 小部件库。已知可以使用 CPython3 和 Pycopy(后者正式支持 Unix 版本),但应该与任何允许访问 stdin/stdout 文件描述符的 Python3 实现一起使用。

The author also goes on to compare PicoTUI against Urwid. Some interesting points there. I am hoping to use PicoTUI for a project I am looking to start. Admittedly, no first hand experience. Came across this question when looking for answers for my TUI (Text-based UI or Terminal-based UI, much in GUI sense) Python library, so thought of adding this option.

作者还继续将 PicoTUI 与 Urwid 进行比较。那里有一些有趣的点。我希望将 PicoTUI 用于我希望开始的项目。诚然,没有第一手经验。在为我的 TUI(基于文本的 UI 或基于终端的 UI,在 GUI 意义上)Python 库寻找答案时遇到了这个问题,所以想到添加这个选项。