Curses-Like Library for Cross-Platform Console App in Python

curses-like library for cross-platform console app in python

There is a wcurses. I've never tried it but it may meet your needs. It sounds like it doesn't have full curses compatibility, but may be close enough. Also it might not be using the DOS terminal, but opening a GUI window and drawing monospaced text inside.

Other windows text mode options are:

  • The console module;
  • wconio -- based on Borland's C conio library.

I believe both are windows only.

looking for x-platform python console library

In case anyone out there is still looking for a curses solution on Windows, here is a site which has PDCurses-based builds of the standard Python curses module for Windows, and covers a pretty wide range: 32-bit and 64-bit, for Python versions 2.5, 2.6, 2.7, 3.1 and 3.2.

PDCurses is "a public domain curses library for DOS, OS/2, Win32, X11 and SDL".

Curses alternative for windows

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.

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.

Screenshot of MinGW and CygWin

Here's a Pyglet GUI example:

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:

Sample Image

Console display in C++ for cross platform

I think NCURSES is what you're looking for. Terminal wrapper for ConsoleUI, which is cross-platform. It allows you to write to a virtual screen buffer, and control when and how the refresh is being done. It does internal book-keeping of that buffer and will only refresh the characters which changed. Curses was designed in days where connection speed mattered so terminal refresh operations were being done in a smart way.

There's an ncurses tag at SO, so you'll find a lot of information here about the gritty details.

Python command line interaction library?

You can control the Unix terminal with the curses library. The library essentially lets you build a simple terminal GUI.

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

Python TUI libs

You might want to check out Unicurses, which wraps the python core curses module in UNIX systems and wraps the free pdcurses library on windows-based machines.

This library is designed to emulate the syntax and style of the original ncurses library, so if you're interested in learning TUI design using curses-style programming, then check it out.

Urwid is, from what little documentation I've read, a very interesting library which uses event loops (the reactor pattern) as a basis for application design, much like tkinter or Twisted. Also, urwid has an event loop class specifically designed for use with Twisted, so if your aim is to created TUIs for use over networks, its a good choice. Twisted also uses the reactor pattern, so if you want to learn that style, I'd recommend it.

Finally, if you do go with an ncurses-style library, check out Dan Gookin's book on ncurses. A very good resource, and the only one I know of which is in print today.

There are other options such as newt, pygcurses, and so on, but this should get you started. Best of luck, TUI programming today is one of those technological fetishes that can be hard to break into, but it's rewarding.

Do not disturb user input while displaying messages to stdout

As far as I can tell, you have a few options here. Normally just calling getch you're running the terminal in what's called "cooked" mode. This means that the terminal just displays characters in the order they arrive, nothing too special. However you run in to race conditions as you've discovered.

Option 1: you read things in one character at a time, buffering the entire line. When a new line arrives and you want to print it out, you'd have to (a) grab hold of some kind of print mutex (internal to the program), (b) clear out the current line (print '\r[space][space][space]...\r'), (c) print the incoming line + '\n', and (d) restore the previous buffer to the screen (via print) and unlock the mutex.

This gets ugly. Fast. As you discovered, there's no line editing support or anything fancy. The only good thing about this approach is that it'll probably work in about 99% of terminals.

Option 2: you put the terminal in to raw mode. In this mode, you have complete control over the terminal, but you lose very basic functionality (e.g. typing a key won't display it unless you manually output it). You would still have to manually implement things like navigation, but at least it would work. This method is probably the most flexible and give you the most control, however it's very difficult to work like this, which is why they invented...

Option 3: you use ncurses (or a similar terminal library). The concepts are a little difficult, but FAR easier to learn than doing things in pure raw mode. Editing and windowing (or as you put it, reserving a line) are built-in. The interface can be much prettier than you'd get from cooked mode.

EDIT I'm talking a lot about *nix terminals here, most of which doesn't apply for Windows.

Add/remove characters in python?

Look at curses module.

See also: curses-like library for cross-platform console app in python



Related Topics



Leave a reply



Submit