Outils pour utilisateurs

Outils du site


issue180:python

Ceci est une ancienne révision du document !


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 80×24 character terminals. Today, the word “TUI” is very popular and means “Textual User Interface”, and there are many popular Python libraries to make things easier to “Pretty up” the console applications. Most of them use the curses Python library in one form or fashion. I looked at many of the available packages and while most of them did a good job, they were very complicated, and, if I’m completely honest, way more flash than substance. 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 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. The initial demo program I wrote is still a good starting point for “modern” discussions of curses. Let’s take a quick look at the code (top right). You can see that there are almost as many commented lines as there are actual lines of code. When we run this code we see the image below. Pretty boring but there are important concepts presented here. To use curses, you must first import the library, then you need to create a virtual screen. 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. myscreen.border(0) myscreen.addstr(12, 25, “See Curses, See Curses Run!”) myscreen.refresh() The program then waits for the user to press a key (any key in this case), then we reset the terminal back to the way it was before we started by calling the endwin() method. myscreen.getch() curses.endwin() 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. import curses Next (below) we’ll create a function that will determine the length of a string and return a value for the horizontal position (x position) so the string will be centered in the terminal window. 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()

Now, here is the catch when using colors. Curses wants you to set up a foreground/background pair and assign it a number, rather than define it on the fly. This requires you to remember which number has the combination you want when you need to use it. That’s a bit of a pain, but that’s how it is. curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_WHITE) curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK) At this point, we can start placing our text within the screen object. Since we are going to centre these strings, it makes it easier to create the string, then pass it to the centre function to get the x (column) position and then use it in the addstr method. Notice that if you want to use some of the special attributes like bold or underline, you can use the bitwise OR pipe character to add them at the end of the addstr command. 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), strng, curses.A_BLINK) screen.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. c = screen.getch() curses.endwin() I have run out of time for this edition of ‘Python in the REAL world’. Ronnie was kind enough to give me extra time to get this done, since I have been having some more health issues. I wasn’t able to create the MicroThisMicroThat article for this month, so please forgive me. I hope I can get it finished for next month’s issue. The code for the two demos will be on my repository at https://github.com/gregwa1953/FCM-180

Encart de la page 32 : The next thing, we’ll demonstrate each of the various attributes you can set for a display string.

issue180/python.1651778046.txt.gz · Dernière modification : 2022/05/05 21:14 de d52fr