issue180:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue180:python [2022/04/30 20:45] – créée d52fr | issue180:python [2022/05/09 13:55] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | A few weeks ago, I was chatting with a new user on the PAGE Discord forum, comparing our careers in programming and how things had changed. I told him how, back in the late 1980s, I worked on a project to create windows and forms for Turbo Pascal and Modula-2. Back then, everything was pretty much done in 80x24 character terminals. | + | **A few weeks ago, I was chatting with a new user on the PAGE Discord forum, comparing our careers in programming and how things had changed. I told him how, back in the late 1980s, I worked on a project to create windows and forms for Turbo Pascal and Modula-2. Back then, everything was pretty much done in 80x24 character terminals. |
Today, the word “TUI” is very popular and means “Textual User Interface”, | Today, the word “TUI” is very popular and means “Textual User Interface”, | ||
- | I went back through the articles that I wrote in the past and found the two articles that I wrote for Full Circle back in 2010 on Curses, so I thought I’d revisit them. | + | I went back through the articles that I wrote in the past and found the two articles that I wrote for Full Circle back in 2010 on Curses, so I thought I’d revisit them.** |
- | Back to Curses | + | |
+ | Il y a quelques semaines, je discutais avec un nouvel utilisateur sur le forum Discord de PAGE, comparant nos carrières dans la programmation et la façon dont les choses avaient changé. Je lui ai raconté comment, à la fin des années 1980, j' | ||
+ | |||
+ | Aujourd' | ||
+ | |||
+ | Je suis retourné dans les articles que j'ai écrits dans le passé et j'ai trouvé les deux articles que j'ai écrits sur Curses pour le Full Circle en 2010, alors j'ai pensé les revisiter. | ||
+ | |||
+ | **Back to Curses | ||
Python for Windows doesn’t include the curses module, but there are libraries that can basically do the same thing. Since we are Linux based here, we don’t have to worry. So let’s get started. | Python for Windows doesn’t include the curses module, but there are libraries that can basically do the same thing. Since we are Linux based here, we don’t have to worry. So let’s get started. | ||
Ligne 18: | Ligne 25: | ||
To use curses, you must first import the library, then you need to create a virtual screen. | To use curses, you must first import the library, then you need to create a virtual screen. | ||
+ | |||
+ | import curses | ||
+ | |||
+ | myscreen = curses.initscr()** | ||
+ | |||
+ | Retour à Curses | ||
+ | |||
+ | Python pour Windows n' | ||
+ | |||
+ | Le programme de démonstration initial que j'ai écrit est toujours un bon point de départ pour les discussions « modernes » sur curses. Jetons un rapide coup d'œil au code (en haut à droite). | ||
+ | |||
+ | Vous pouvez voir qu'il y a presque autant de lignes commentées qu'il y a de lignes de code réelles. | ||
+ | |||
+ | Lorsque nous exécutons ce code, nous voyons l' | ||
+ | |||
+ | Plutôt ennuyeux, mais il y a des concepts importants présentés ici. | ||
+ | |||
+ | Pour utiliser curses, vous devez d' | ||
import curses | import curses | ||
Ligne 23: | Ligne 48: | ||
myscreen = curses.initscr() | myscreen = curses.initscr() | ||
- | Next, we draw a border around the screen and add a line of text starting at row 12, column 25. The refresh() method actually shows what has been done to the screen. To see the new TUI screen, you MUST call the refresh() method. | + | |
+ | **Next, we draw a border around the screen and add a line of text starting at row 12, column 25. The refresh() method actually shows what has been done to the screen. To see the new TUI screen, you MUST call the refresh() method. | ||
myscreen.border(0) | myscreen.border(0) | ||
Ligne 37: | Ligne 63: | ||
curses.endwin() | curses.endwin() | ||
- | Nothing big, nothing special. Just a quick and dirty demo. So let’s try to do something better. | + | Nothing big, nothing special. Just a quick and dirty demo. So let’s try to do something better.** |
- | This time, we’ll create a demo program showing how to centre a line in the terminal and show all of the various attributes that can be set to provide different effects. First off, we need to import the curses library. | + | Ensuite, nous dessinons une bordure autour de l' |
+ | |||
+ | myscreen.border(0) | ||
+ | |||
+ | myscreen.addstr(12, | ||
+ | |||
+ | myscreen.refresh() | ||
+ | |||
+ | Le programme attend ensuite que l' | ||
+ | |||
+ | myscreen.getch() | ||
+ | |||
+ | curses.endwin() | ||
+ | |||
+ | Rien de grand, rien de spécial. Juste une démo rapide et sale. Essayons donc de faire quelque chose de mieux. | ||
+ | |||
+ | |||
+ | **This time, we’ll create a demo program showing how to centre a line in the terminal and show all of the various attributes that can be set to provide different effects. First off, we need to import the curses library. | ||
import curses | import curses | ||
Ligne 46: | Ligne 89: | ||
Now we can get curses initialized and get the number of rows and columns of the terminal. This allows us to use the program in any size terminal that you can use. Notice the line right after the initscr() line. This allows us to use colors in our strings. If you don’t set this, colors won’t work, and all the documentation on curses says that this call should be made “as soon after the initialization as possible”. We also get the number of rows and columns and place them into global variables so we can use the num_cols variable in the centre function. | Now we can get curses initialized and get the number of rows and columns of the terminal. This allows us to use the program in any size terminal that you can use. Notice the line right after the initscr() line. This allows us to use colors in our strings. If you don’t set this, colors won’t work, and all the documentation on curses says that this call should be made “as soon after the initialization as possible”. We also get the number of rows and columns and place them into global variables so we can use the num_cols variable in the centre function. | ||
+ | |||
+ | screen = curses.initscr() | ||
+ | |||
+ | curses.start_color() | ||
+ | global num_rows, num_cols | ||
+ | |||
+ | num_rows, num_cols = screen.getmaxyx()** | ||
+ | |||
+ | Cette fois-ci, nous allons créer un programme de démonstration expliquant comment centrer une ligne dans le terminal, et montrer tous les différents attributs qui peuvent être définis pour fournir différents effets. Tout d' | ||
+ | |||
+ | import curses | ||
+ | |||
+ | Ensuite (ci-dessous), | ||
+ | |||
+ | Maintenant nous pouvons initialiser curses et obtenir le nombre de lignes et de colonnes du terminal. Cela nous permet de nous servir du programme avec n' | ||
screen = curses.initscr() | screen = curses.initscr() | ||
Ligne 54: | Ligne 112: | ||
num_rows, num_cols = screen.getmaxyx() | num_rows, num_cols = screen.getmaxyx() | ||
- | Now, here is the catch when using colors. Curses wants you to set up a foreground/ | + | |
+ | **Now, here is the catch when using colors. Curses wants you to set up a foreground/ | ||
curses.init_pair(1, | curses.init_pair(1, | ||
Ligne 64: | Ligne 123: | ||
One of the last things we need to do is to prompt the user to press a key to end the demo, so we centre the string and place it near the bottom of the screen and make it blink. | One of the last things we need to do is to prompt the user to press a key to end the demo, so we centre the string and place it near the bottom of the screen and make it blink. | ||
+ | strng = "Press a key to end demo..." | ||
+ | screen.addstr(num_rows - 3, centre(strng), | ||
+ | screen.refresh()** | ||
+ | Maintenant, voici le problème de l' | ||
- | strng = "Press a key to end demo..." | + | curses.init_pair(1, |
+ | |||
+ | curses.init_pair(2, | ||
+ | |||
+ | A ce stade, nous pouvons commencer à placer notre texte dans l' | ||
+ | |||
+ | L'une des dernières choses que nous devons faire est d' | ||
+ | |||
+ | strng = "Press a key to end demo..." | ||
screen.addstr(num_rows - 3, centre(strng), | screen.addstr(num_rows - 3, centre(strng), | ||
- | screen.refresh() | + | écran.refresh() |
- | Finally, we use the getch() function to wait with a blocking call until any keyboard character is pressed. Notice we don’t have to put a specific location for the cursor, since curses remembers the last position that was used, which is when we print the “Press a key to end demo…” line. Once the user presses a key, we release the terminal screen. | + | **Finally, we use the getch() function to wait with a blocking call until any keyboard character is pressed. Notice we don’t have to put a specific location for the cursor, since curses remembers the last position that was used, which is when we print the “Press a key to end demo…” line. Once the user presses a key, we release the terminal screen. |
c = screen.getch() | c = screen.getch() | ||
Ligne 84: | Ligne 155: | ||
- | The code for the two demos will be on my repository at https:// | + | The code for the two demos will be on my repository at https:// |
+ | |||
+ | Enfin, nous utilisons la fonction getch() pour attendre avec un appel bloquant qu'un caractère du clavier soit enfoncé. Remarquez que nous n' | ||
+ | |||
+ | c = screen.getch() | ||
+ | |||
+ | curses.endwin() | ||
+ | |||
+ | Je suis à court de temps pour cette édition de « Python dans le monde réel ». Ronnie a eu la gentillesse de me donner du temps supplémentaire pour le faire, puisque j'ai eu d' | ||
+ | |||
+ | |||
+ | Le code des deux démos sera sur mon dépôt à https:// | ||
+ | |||
+ | |||
+ | |||
+ | // Encart de la page 32 : // | ||
+ | ** The next thing, we’ll demonstrate each of the various attributes you can set for a display string.** | ||
+ | |||
+ | Ensuite, nous allons démontrer chacun des différents attributs que vous pouvez définir pour une chaîne d' | ||
+ |
issue180/python.1651344332.txt.gz · Dernière modification : 2022/04/30 20:45 de d52fr