Python 适用于 Windows 的 Curses 替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14779486/
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
Curses alternative for windows
提问by Chandan
Is there any alternative of the curses module for python to use in windows? I looked up in the python documentation, but there its mentioned that its for using in unix. I am not much familiar with these, so is there some way to use curses module in windows or is there some similar module specially for windows? [I am using Python 3.3]
在 Windows 中,python 是否有任何可用的 Curses 模块替代方案?我在 python 文档中查找,但它提到它用于在 unix 中使用。我对这些不太熟悉,那么有没有办法在windows中使用curses模块,或者是否有一些专门用于windows的类似模块?[我使用的是 Python 3.3]
采纳答案by Torxed
Then you're out of luck i'm afraid. There's no real cross-platform version or port of curses/ncurses, there is a "dialogue" port which works, but it's limited in capabilities.
那你恐怕就不走运了。没有真正的跨平台版本或 curses/ncurses 的端口,有一个“对话”端口可以工作,但功能有限。
Your best bet is to run CygWin or MinGW32, both are, in "loose terms", a Linux system+terminal emulator which has much of the binaries you need. They can run native Linux/Unix binaries inside the terminal and access your "host" system files at any time, so it's like patching Windows with a kick-ass terminal with all your goodies from the Linux world. You'll still need some basic knowledge of Linux and how the commands etc work, but you'll figure it out.
最好的办法是运行 CygWin 或 MinGW32,在“宽松的术语”中,两者都是 Linux 系统+终端模拟器,其中包含许多您需要的二进制文件。他们可以在终端内运行本地 Linux/Unix 二进制文件并随时访问您的“主机”系统文件,所以这就像使用来自 Linux 世界的所有好东西用踢屁股终端修补 Windows。您仍然需要一些 Linux 的基本知识以及命令等的工作原理,但您会弄清楚的。


Here's a Pyglet GUI example:
这是一个 Pyglet GUI 示例:
import pyglet
from pyglet.gl import *
class main (pyglet.window.Window):
def __init__ (self):
super(main, self).__init__(800, 600, fullscreen = False)
self.button_texture = pyglet.image.load('button.png')
self.button = pyglet.sprite.Sprite(self.button_texture)
## --- If you'd like to play sounds:
#self.sound = pyglet.media.load('music.mp3')
#self.sound.play()
self.alive = 1
def on_draw(self):
self.render()
def on_close(self):
self.alive = 0
def on_mouse_press(self, x, y, button, modifiers):
if x > self.button.x and x < (self.button.x + self.button_texture.width):
if y > self.button.y and y < (self.button.y + self.button_texture.height):
self.alive = 0
def on_key_press(self, symbol, modifiers):
if symbol == 65307: # [ESC]
self.alive = 0
def render(self):
self.clear()
self.button.draw()
self.flip()
def run(self):
while self.alive == 1:
self.render()
# -----------> This is key <----------
# This is what replaces pyglet.app.run()
# but is required for the GUI to not freeze
#
event = self.dispatch_events()
x = main()
x.run()
Here's the output of that code:
这是该代码的输出:


回答by ashes999
I'm happy to report that there's now a Windows build of Curses available as an extension for Python on Windows, from here. (I didn't write it, and I don't know who maintains it.)
我很高兴地报告,现在有一个 Windows 版本的 Curses 可以作为 Windows 上 Python 的扩展,从这里。(不是我写的,也不知道是谁维护的。)
You can run the installer, and import cursesto get curses running. (Verified on 64-bit Windows 7 and Windows 8.)
您可以运行安装程序,并import curses运行curses。(已在 64 位 Windows 7 和 Windows 8 上验证。)
@ArtOfWarfare points out that you can install this via Pip with this commend:
@ArtOfWarfare 指出,您可以通过 Pip 使用以下命令安装它:
pip install http://www.lfd.uci.edu/~gohlke/pythonlibs/xugyqnq9/curses-2.2-cp27-none-win32.whl
回答by ArtOfWarfare
Here's how to install what ashes999 linked to in their answervia pip:
以下是如何通过 pip在他们的答案中安装ashes999 链接的内容:
pip install http://www.lfd.uci.edu/~gohlke/pythonlibs/xugyqnq9/curses-2.2-cp27-none-win32.whl
This should probably be added to PyPI to make installation with pip even easier (so it could be installed by name rather than URL.)
这可能应该添加到 PyPI 中,以便使用 pip 安装更容易(因此它可以通过名称而不是 URL 安装。)
回答by Peter Brittain
The original question was whether there is an alternativeto curses on Windows.
最初的问题是在 Windows 上是否有替代curses的方法。
One answer is to use the Win32 console API. You can program this directly in Python using the excellent pywin32 package if you're already familiar with the console API.
一种答案是使用Win32 控制台 API。如果您已经熟悉控制台 API,您可以使用优秀的 pywin32 包直接在 Python 中进行编程。
However, I found this too low level for my recent project. I was also less than keen on forcing my users to build/install PDcurses, and besides, I also find curses too low level for a modern OO language like Python too.
但是,我发现这对于我最近的项目来说太低了。我也不太热衷于强迫我的用户构建/安装 PDcurses,此外,我还发现对于像 Python 这样的现代 OO 语言来说,curses 的级别太低了。
I have therefore put together a high level cross-platform API to do all the things most people want from their terminal/console. The asciimaticspackage will provide most of your input and output needs. If you're on Linux this is a more human way to program curses. If you're on Windows, the same class works as is with no external binary dependencies. See below for an example screenshot:
因此,我将一个高级跨平台 API 放在一起,以完成大多数人希望从他们的终端/控制台完成的所有事情。该asciimatics包将提供大部分的输入和输出需求。如果你在 Linux 上,这是一种更人性化的方法来编程诅咒。如果您使用的是 Windows,则相同的类可以在没有外部二进制依赖项的情况下工作。请参阅下面的示例屏幕截图:
There are many other effects and widgets available which you can find in the gallery, but if there's an extra feature you need, let me know and I'll see what I can do.
您可以在图库中找到许多其他效果和小部件,但如果您需要额外的功能,请告诉我,我会看看我能做些什么。
回答by Flo
You may try this one. I once did the Win64-port for this (merged in there). You however need to write your Python code a bit different. This one will redirect all curses calls to the native Python version on UNIX, but call PDCURSES.DLL on Windows (download the DLL separately). It supports unicode as far as I remember:
你可以试试这个。我曾经为此做过 Win64 端口(合并在那里)。但是,您需要编写稍微不同的 Python 代码。这会将所有curses 调用重定向到UNIX 上的本机Python 版本,但在Windows 上调用PDCURSES.DLL(单独下载DLL)。据我所知,它支持 unicode:
回答by jeromej
The official doc proposes the following (hereat the bottom of the paragraph):
官方文档提出以下建议(在段落底部):
The Windows version of Python doesn't include the cursesmodule. A ported version called UniCursesis available. You could also try the Console modulewritten by Fredrik Lundh, which doesn't use the same API as curses but provides cursor-addressable text output and full support for mouse and keyboard input.
Python 的 Windows 版本不包含curses模块。可以使用名为UniCurses的移植版本。您还可以尝试Fredrik Lundh 编写的 Console 模块,它不使用与 curses 相同的 API,但提供光标可寻址的文本输出并完全支持鼠标和键盘输入。
回答by Lossan
This is not a new solution, just an edit to a previous one.
这不是一个新的解决方案,只是对前一个解决方案的编辑。
In case the pipcommand is giving a 404 error you can try to download the packet from http://www.lfd.uci.edu/~gohlke/pythonlibs/#cursesand then write something like
如果pip命令给出 404 错误,您可以尝试从http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses下载数据包 ,然后编写类似
pip install C:\..packetPath..\curses-2.2-cp35-none-win_amd64.whl

