How to create a static ASCII interface in Python?

When I use the less command in my Mac terminal, I show 23 line slices of the specified file. If I go down the file, then scroll in the terminal window, I don’t see any file contents in front of the current fragment. Instead, I see the commands I typed before using less .

I would like to use this or similar effect to create an ascii game interface that only shows the current screen with no history. I would draw my frames on the screen and change only the text or parameters in these frames. This is very common on older systems that work mostly or completely in a command line environment.

Is there a Python module that offers this? Is this an effect that I can or must realize myself?

Here are some examples of screen elements that can benefit from this effect.

 +-------------------------------------------------------------------+ | | | Dialog dialog dialog dialog dialog dialog dialog dialog dialog | | dialog dialog dialog dialog dialog dialog dialog dialog dialog | | dialog dialog dialog dialog dialog dialog dialog dialog dialog | | dialog dialog dialog dialog dialog dialog dialog dialog dialog | | dialog dialog dialog dialog. | | | | | | | +--------------------+---------------+------------------------------+ | Attack | South | HP/Max MP/Max | | > Cast Magic | - | Tom: 120/120 60/91 | | Use Item | New | > Dick: 27/133 47/47 | | Tactics | Schwartzton | Harry: 87/104 16/113 | +--------------------+---------------+------------------------------+ 
+6
source share
4 answers

I would suggest learning the curses module. This is the interface of the ncurses , which is used precisely for this purpose (I believe that many terminal applications are written in it). The documentation is pretty decent, and there are some good tutorials that should also set you in the right direction.

(Either this or the liberal use of \r .) I would strongly suggest the curses module, as it should provide more flexibility for what you want to do.

EDIT: If you are interested in creating a cross-platform application, keep in mind that the curses module is only available on * nix systems. According to the documentation:

No one made a Windows port for the curses module.

This tutorial recommends this library for use on Windows, although if the dates on the download page are any indication, it has not been updated in a fair amount of time. You can study this module , which looks much more recent.

+6
source

What you are talking about is curses . This is a Python module that interacts with ncurses. The best tutorial I've found to use curses is this one: http://www.dev-explorer.com/articles/python-with-curses

Some popular programs that use curses (albeit from a different language) are aptitude and nano.

+3
source

I am also struggling with this problem. I just wanted a cross platform solution, so I added asciimatics to provide a set of widgets for creating forms-based user interfaces. For instance:

Text Interface Widgets

The resulting code should be concise enough and work on any modern OS (including Windows, Linux and OSX) without installing additional proprietary libraries. As you can see from the example contact list, standard widgets are easy to lay out and should provide most of your needs.

+2
source

If you want to make the game ascii, asceticism probably won't work, as it is mainly for things like forms. Using curses is definitely the way to go. Although there is no official damn port for windows, there is a nice unofficial option https://www.lfd.uci.edu/%7Egohlke/pythonlibs/#curses , so be sure to check this out.

-1
source

Source: https://habr.com/ru/post/919445/


All Articles