Curses for python windows

Curses in Windows with Python

This post only discusses how to get curses working in Windows. A full curses tutorial is also available. . For a tutorial on how to use curses, check out Curses Programming in Python.

The curses package is part of the Python standard library and is useful for creating text-based user interfaces and generally controlling the screen and keyboard input. The big problem is that it doesn’t work out-of-the-box on Windows. This show you how to get curses working in Windows. This was tested in Windows 10 with Python 3.6. This gets the screen control, keyboard input, and colors all working. It uses the windows-curses package.

The curses library goes back decades and is used to control terminal/shell output. Read more about the history on the Wikipedia curses page. This is not a full tutorial on curses programming. Refer to the official documentation, Curses Programming with Python. That will teach you how to actually use the library.

You might also be interested in the tutorial Colorize Terminal Output in Python.

In Windows 10, some color escape codes are supported without any special configuration. Read more at MSDN Console Virtual Terminal Sequences. It is not full curses support but some colors and cursor control abilities work.

Читайте также:  (.*)

Install windows-curses package

You can use pip to install the windows-curses package. That’s really all there is to it! This package will make it so you can use the Python standard curses module in Windows. From your command prompt or shell, run pip install or preferably python -m pip install like this:

python -m pip install windows-curses

Test out curses

After installing the windows-curses package using pip, you can test it out with code:

# Clear the screen and hold it for 3 seconds
import curses
import time

screen = curses.initscr()
screen.clear()

time.sleep(3)

Curses tutorial

For a tutorial on how to use curses, check out Curses Programming in Python.

Conclusion

After reading this, you should know how to get curses applications running in Windows. To learn more about how to use curses in your applications, refer to Curses Programming with Python (official documentation). You might also be interested in the tutorial Colorize Terminal Output in Python.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Windows Curses Python module

License

zephyrproject-rtos/windows-curses

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Python curses wheels for Windows

This is the repository for the windows-curses wheels on PyPI. The wheels are based on the wheels on Christoph Gohlke's page.

Only build-wheels.bat is original work.

Wheels built from this repository can be installed with this command:

pip install windows-curses 

Starting with version 2.0, these wheels include a hack to make resizing work for Python applications that haven't been specifically adapted for PDCurses. See this commit. The description on PyPI has a longer explanation.

Note that this hack is not in Gohlke's wheels.

This project is not actively maintained and is looking for maintainers.

If you are interested, please let us know by either creating an issue here or messaging in the #windows-support channel on Zephyr Discord.

The curses module is in the Python standard library, but is not available on Windows. Trying to import curses gives an import error for _curses , which is provided by Modules/_cursesmodule.c in the CPython source code.

The wheels provided here are based on patches from https://bugs.python.org/issue2889, which make minor modifications to _cursesmodule.c to make it compatible with Windows and the PDCurses curses implementation. setup.py defines HAVE_* macros for features available in PDCurses and makes some minor additional compatibility tweaks.

The patched _cursesmodule.c is linked against PDCurses to produce a wheel that provides the _curses module on Windows and allows the standard curses module to run.

The wheels are built with wide character support and force the encoding to UTF-8. Remove UTF8=y from the nmake line in build-wheels.bat to use the default system encoding instead.

git clone --recurse-submodules https://github.com/zephyrproject-rtos/windows-curses.git 
py -3.6 -m pip install --upgrade wheel setuptools 

If you have both 32-bit and 64-bit versions of the same Python version installed and are building a 32-bit wheel, add "-32" to the version number, like in the following example:

If you are building multiple wheels for Python versions that are all compatible with the same compiler, you can list all of them in the same command:

Rebuilding the wheels for Python 3.6, 3.7, 3.8, 3.9, 3.10, and 3.11

In x86 Native Tools Command Prompt for VS 2022 :

build-wheels.bat 3.6-32 3.7-32 3.8-32 3.9-32 3.10-32 3.11-32 

In x64 Native Tools Command Prompt for VS 2022 :

build-wheels.bat 3.6 3.7 3.8 3.9 3.10 3.11 

This gives a set of wheels in dist\ .

This building scheme above should be the safest one to use. In practice, many of the resulting wheels seem to be forwards- and backwards-compatible.

  1. Bump the version number in setup.py according to the Semantic versioning.
  2. Create a Git tag for the release:
git tag -s -m "windows-curses 1.2.3" v1.2.3 git push upstream v1.2.3 

Once a GitHub release is created, the GitHub Actions CI will automatically build and upload the wheels to the PyPI.

NOTE: The process of uploading wheels for releases is automated using the GitHub Actions and manual uploads should not be necessary under normal circumstances.

Don't forget to bump the version number in setup.py before building new wheels. Semantic versioning is intended.

Once the wheels are built, follow the instructions here to upload them to PyPI.

pip /PyPI will look at the wheel metadata and automatically install the right version of the wheel.

Adding support for a new Python version

  1. Create a new directory for the Python version, e.g. py39\
  2. Copy Modules\_cursesmodule.c from the CPython source code to py39\_cursesmodule.c
  3. Apply the PDCurses compatibility patch from this commit and the resizing hack from this commit to the new py39\_cursesmodule.c .
  4. Copy Modules\_curses_panel.c , Modules\clinic\_cursesmodule.c.h , and Modules\clinic\_curses_panel.c.h from the CPython sources to py39\_curses_panel.c , py39\clinic\_cursesmodule.c.h and py39\clinic\_curses_panel.c.h , respectively
  5. Add the build specifications for the new Python version in .github/workflows/ci.yml .

In practise, Modules\_cursesmodule.c from newer Python 3 versions is likely to be compatible with older Python 3 versions too. The Python 3.6 and 3.7 wheels are currently built from identical _cursesmodule.c files (but not the Python 3.8 or 3.9 wheels).

For Python 3.10 and 3.11 it is necessary to adapt _cursesmodule.c and clinic\_cursesmodule.c.h files to new Python API (decribed more here https://devguide.python.org/c-api). It demands removing two headers files as described in this commit.

About

Windows Curses Python module

Источник

Оцените статью