issue169:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue169:python [2021/06/01 07:37] – créée d52fr | issue169:python [2021/06/05 16:10] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | 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. | + | **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. | + | 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? | + | Comme d' |
+ | |||
+ | Ainsi, ce mois-ci, je vous montrerai comment utiliser la bibliothèque bext pour contrôler l' | ||
+ | |||
+ | **What is bext? | ||
Bext, according to its homepag, is: “Basically, | Bext, according to its homepag, is: “Basically, | ||
Ligne 13: | Ligne 17: | ||
pip install bext | 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. | + | 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 | + | Qu' |
+ | |||
+ | D' | ||
+ | |||
+ | Puisque de plus en plus d' | ||
+ | |||
+ | Pour installer bext, tout ce qu'il faut faire est d' | ||
+ | |||
+ | pip install bext | ||
+ | |||
+ | Une fois que bext est installée, vous pouvez commencer à l' | ||
+ | |||
+ | **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… | 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… | ||
Ligne 21: | Ligne 37: | ||
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. | 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()), | + | Next, we need to do some setup. We start by setting the background color (bext.bg()) and foreground color (bext.fg()), |
- | Then (next page, top right) we get the width and height of the terminal using bext.size(), | + | Projet de programme n° 1 |
+ | |||
+ | D' | ||
+ | |||
+ | Comme toujours, il faut importer bext. Puisque le programme ne fonctionnera pas sans elle, j' | ||
+ | |||
+ | Ensuite, il faut faire un peu de paramétrage. Nous commençons par régler la couleur de l' | ||
+ | |||
+ | **Then (next page, top right) we get the width and height of the terminal using bext.size(), | ||
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). | 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… | + | 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. | + | Ensuite (page suivante, en haut à droite), nous réglerons la largeur et la hauteur du terminal avec bext.size(), |
+ | |||
+ | Remarquez que nous imprimons la largeur et la hauteur de l' | ||
+ | |||
+ | Nous utilisons la commande bext.goto() qui prend deux paramètres, | ||
+ | |||
+ | **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:// | ||
- | Bext has only a few commands, and you can find a list of them on the bext homepage at | ||
- | https:// | ||
Now that we have some very basic understanding of the bext library, let’s move on to something a bit more complex. | 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 | + | 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, | + | Si vous utilisez Gnome Terminal, vous pouvez changer la taille du terminal en cliquant sur Terminal dans le menu principal, puis sur la taille de la fenêtre du terminal. Je suis certain que la plupart des autres terminaux ont le même type d' |
+ | |||
+ | Bext n'a que quelques commandes et vous pouvez en trouver une liste sur la page d' | ||
+ | |||
+ | Maintenant que vous avez une très basique compréhension de la bibliothèque bext, avançons à quelque chose d'un peu plus compliqué. | ||
+ | |||
+ | Projet de programme n° 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, | ||
This time, we’ll simply import the bext library | This time, we’ll simply import the bext library | ||
Ligne 44: | Ligne 82: | ||
import bext | 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 (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. | + | Si vous pouvez vous souvenir de l' |
+ | |||
+ | Cette fois-ci, nous allons importer la bibliothèque bext de façon simple. | ||
+ | |||
+ | import bext | ||
+ | |||
+ | Maintenant (ci-dessous), | ||
+ | |||
+ | **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... | 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. | + | 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.** |
+ | |||
+ | Maintenant (en haut et à droite de la page suivante), nous créons quelques listes qui contiendront les positions X,Y pour chaque invite dans la forme, le texte qui sera imprimé pour chacune, et les positions X,Y du curseur pour qu'il reçoive les entrées. | ||
+ | |||
+ | Maintenant (ci-dessous), | ||
+ | |||
+ | Remarquez que, avant d' | ||
- | Save your program as “Bext_test1.py”, | + | **Save your program as “Bext_test1.py”, |
That’s it. You can find the code for the two projects at my github repository https:// | That’s it. You can find the code for the two projects at my github repository https:// | ||
Ligne 59: | Ligne 111: | ||
Next month, we’ll take a look at extracting text from a PDF file. | 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! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | Sauvegardez votre programme comme « Bext_test1.py » et lancez-le dans un terminal. Il devrait ressembler à ceci : | ||
+ | |||
+ | Et voilà. Vous trouverez le code pour les deux projets dans mon dépôt github https:// | ||
+ | |||
+ | Soit dit en passant, c'est Al Sweigart qui a écrit la bibliothèque bext. J'ai fait une critique de son nouveau livre dans ce numéro du FCM. | ||
+ | |||
+ | Le mois prochain, nous regarderons comment extraire du texte d'un fichier PDF. | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | |||
+ | //Page 34, encart en bas à droite, lignes noires au milieu :// | ||
+ | **Next, we’ll create a function that will set the colors for foreground and background, and take a parameter to invert those colors ( swap the foreground and background colors)** | ||
+ | |||
+ | Ensuite nous créerons une fonction qui règlera les couleurs d' | ||
+ | |||
+ | //Page 35, encart en haut à droite, lignes noires au milieu :// | ||
+ | **At this point, we’ll create a function to fill the screen with our prompts.** | ||
+ | |||
+ | Ici, nous créerons une fonction qui remplit l' | ||
+ | |||
+ | //Page 35, encart en bas à gauche, lignes noires au milieu :// | ||
+ | **Finally, we call our functions. Notice that we call SetUpScreen() with False as the parameter to set the text colors to a black background and green foreground.** | ||
+ | Enfin nous appelons nos fonctions. Notez que nous appelons SetUpScreen() avec Falsecomme paramètre pour régler les couleurs du texte avec arrière-plan noir et texte vert. |
issue169/python.1622525859.txt.gz · Dernière modification : 2021/06/01 07:37 de d52fr