Creating a text based user interface with Python and Curses

Retro look and highly functional

Datagod's photo
Datagod
·Sep 11, 2021·

3 min read

Creating a text based user interface with Python and Curses

As I work on my MeshTalk project, the most difficult part is not learning new commands or tools but simply stopping and taking the time to blog about it.

I get so excited to see progress that I just want to keep going. Good for me, bad for you.

So over the last few evenings I have added a lot of code to my tiny project. I wanted to make an interesting user interface, so I borrowed some code from another one of my projects called GPSProbe.

Curses

Creating text based user interfaces is nothing new. Back in the day, mainframe computers were accessed via dumb terminals that would display text based graphics. The applications running were menu driven and controlled by the keyboard.

From their article Curses Programming with Python, the author's state:

The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals; such terminals include VT100s, the Linux console, and the simulated terminal provided by various programs. Display terminals support various control codes to perform common operations such as moving the cursor, scrolling the screen, and erasing areas. Different terminals use widely differing codes, and often have their own minor quirks.

2021-09-11 12_30_33-meshtalk.py - meshtalk [SSH_ meshtasticPi.local] - Visual Studio Code.jpg

On my GPSProbe project, I spent a lot of time developing custom functions to allow me to write to the text boxes. I put them into a class called TextWindow

2021-09-11 12_49_23-meshtalk.py - meshtalk [SSH_ meshtasticPi.local] - Visual Studio Code.jpg

This class has several very useful functions that allow me to write to the specified TextWindow, specify the color, and either have the text fixed in place (using a co-ordinate) or have the line of text simply scroll. Scrolling lines get highlighted automatically. This is very useful when you have a lot of text scrolling by, as the text will wrap.

2021-09-11 12_49_45-meshtalk.py - meshtalk [SSH_ meshtasticPi.local] - Visual Studio Code.jpg

2021-09-11 12_50_01-meshtalk.py - meshtalk [SSH_ meshtasticPi.local] - Visual Studio Code.jpg

When using Curses it is important to exit cleanly so the command line display will not end up garbled. You do this by putting your "main" code into a function, then wrapping it with what I call pre-amble code.

2021-09-11 12_51_09-● meshtalk.py - meshtalk [SSH_ meshtasticPi.local] - Visual Studio Code.jpg Here you can see the MeshTalk main code is now in a dedicated function. That function will be called by the pre-amble code. When main is finished, the pre-amble code will shut down the curses environment gracefully, and return the command line display back to normal.

I have also included error handling which will do the same, in case of an error. That will be covered in the next article.

If you have been enjoying this series or have any questions, please drop a comment.

As always, you can find the latest version of the MeshTalk project on GITHub

 
Share this