Ceci est une ancienne révision du document !
As usual, my mind and world has been preoccupied by other things, and my ‘Python in the REAL world’ (again, as usual) ended up being the to-do item falling to the bottom of the list. I’ve been SO focused on playing with the Microcontrollers and MicroPython that time has slipped by (again, as usual) and it’s now time for me to write the article for the Python side of things.
So this month, I will show you how to use the bext library to control the screen in a terminal window, similar to the curses library, but this will also work on Microsoft Windows as well.
What is bext?
Bext, according to its homepag, is: “Basically, use Bext if you want to move the cursor around the terminal window and have colorful text, like some kind of limited curses module (but it works on Windows also.)”
With more and more applications moving to a GUI model, use of “fancy” graphics inside a terminal window is getting more and more rare. However, there is still something to be said for the ability to easily control the cursor, placement of text, and even the color of text within a CLI program.
To install bext, all you have to do is use pip (or pip3)…
pip install bext
Once you have bext installed, you can begin to use it. Let’s create a very simple program to show off its abilities.
Program project #1
First, a small warning about running projects that use bext in your IDE. Since bext controls a terminal positioning and colors, the terminal that your IDE uses might not work correctly. If you happen to use VS Code, the programs will work, but you need to remember to click in the terminal at the bottom of the Code window. If you are using Geany, it also will work, but the terminal window will most likely scroll beyond the actual program output. I haven’t tried IDLE, but I’m guessing that it won’t work. Therefore, create your program in your normal IDE, then open a terminal window to test your program from the command-line. Below is the code for our first bext program…
As always, we need to import bext. Since the program won’t work without it, I use a try | except to wrap the import statement. That way, if it fails, the user knows why immediately.
Next, we need to do some setup. We start by setting the background color (bext.bg()) and foreground color (bext.fg()), then clear the terminal (bext.clear()). When you set the background and foreground colors, you have only a few options to choose from. The allowable list of color choices are black, red, green, yellow, blue, purple, cyan, white, reset, random.
Then (next page, top right) we get the width and height of the terminal using bext.size(), since most terminal programs have the ability to change the width and height of the window.
Notice that we print the width and height of the terminal screen. Once we do this, we need to print on the next line, not the top line, so we will move the cursor to the far left of the screen, the second line (0,1), print something, then move the cursor to column 10, row 6 and print a test line again. Finally, we move the cursor to the bottom line, position 0 and end the program (code is bottom left).
We use the bext.goto() command which takes two parameters, x position (column position) and y position (line of the terminal). Save the program to “bext_demo1.py”. When you run the program in a terminal, it should look something like this…
If you happen to be using Gnome Terminal, you can change the size of the terminal by clicking Terminal from the main menu and then the size of the terminal window. I’m sure that most other terminals have the same sort of option.
Bext has only a few commands, and you can find a list of them on the bext homepage at https://github.com/asweigart/bext.
Now that we have some very basic understanding of the bext library, let’s move on to something a bit more complex.
Program Project #2
If you actually remember the moon landing (1969), you will remember the terminals that we had to use way back then. If not, there were no cell phones, no personal computers, and no internet. Back in those days, computers were all mainframes, and access was via serial terminals – which were just a keyboard and CRT monitor. The monitors had a black background and (usually) green text. There was no mouse, so the programs had to print out a “form” on the screen, then set the cursor to the proper X,Y position for the input. Once the terminal operator entered the correct (hopefully) information, (s)he used the <ENTER> key to move to the next position on the screen and the whole thing repeated, until all the information was entered. The program we will create emulates a very small version of that.
This time, we’ll simply import the bext library
import bext
Now (below) we’ll create a function to take a string, decide how to center it based on the width of the terminal window, and print it at the proper position.
Now (next page, top right) we’ll create some lists to hold the X,Y positions for each prompt in the form, hold the text that will be printed for each, and the X,Y positions for the placement of the cursor to receive the inputs. Now (below) we’ll create a function to handle the input of each data item. We’ll simply use the input() function for this…
Notice that before we call the GetEntry() function to get the data, we call SetUpScreen() function again, but this time, we pass a True, to invert the colours for each input field.
Save your program as “Bext_test1.py”, and run it in a terminal. It should look something like this…
That’s it. You can find the code for the two projects at my github repository https://github.com/gregwa1953/FCM-169.
Just as an aside, the bext library was written by Al Sweigart. I have done a book review on his latest book in this issue of Full Circle.
Next month, we’ll take a look at extracting text from a PDF file.
Until next time, as always; stay safe, healthy, positive and creative!