issue180:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue180:python [2022/05/05 21:14] – d52fr | issue180:python [2022/05/09 13:55] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 4: | Ligne 4: | ||
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.** | ||
+ | |||
+ | |||
+ | 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 | **Back to Curses | ||
Ligne 22: | Ligne 29: | ||
myscreen = curses.initscr()** | 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 | ||
+ | |||
+ | 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. | ||
Ligne 38: | Ligne 64: | ||
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.** | ||
+ | |||
+ | 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. | **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. | ||
Ligne 53: | Ligne 96: | ||
num_rows, num_cols = screen.getmaxyx()** | 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() | ||
+ | |||
+ | curses.start_color() | ||
+ | global num_rows, num_cols | ||
+ | |||
+ | 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/ | ||
Ligne 69: | Ligne 128: | ||
screen.refresh()** | screen.refresh()** | ||
+ | |||
+ | Maintenant, voici le problème de l' | ||
+ | |||
+ | 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), | ||
+ | |||
+ | é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. | ||
Ligne 80: | Ligne 156: | ||
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 : // | // Encart de la page 32 : // | ||
** The next thing, we’ll demonstrate each of the various attributes you can set for a display string.** | ** 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.1651778046.txt.gz · Dernière modification : 2022/05/05 21:14 de d52fr