Update: The follow-up post can be found at The Curse of Python (continues…)
I have been working on developing games in text mode using Python. I have used the Curses library in my code for writing the UI for these games. I plan to share my experience during the development process. This post and the next one will focus on the Curses Library and its usage with Python.
The curses library installation is a two minute job. Of course, there are some additional system requirements.
For Windows: Assuming you have python 2.7+ installed , you can download the .whl file (“wheels” file) from the link below to get the curses library installer:
Then, use the following command to install the curses library:
> python -m pip install <path_to_wheel_file>
Note: if you donot have PIP installed, refer to this post for installation details.
To check if the installation is complete, the following command should not give an error on the python prompt:
>> import curses
For Linux : <TODO> (I have not done a setup yet on Linux, will update this post once I try it!)
Before I start writing the first “hello world” program , let’s talk about what is the curses library.
The Curses library is a collection of APIs that manages an application’s display on command line interfaces, thereby enhancing the way a user interacts with the applications that are intended to run on a terminal instead of a GUI. It provides wrapper for writing solid UI applications in text mode. For example, a small application to setup and apply firewall rules.Such an application can be implemented using the curses library. It liberates you from writing a long iptables commands!
The term “Curses” is actually a pun on the name “cursor optimization“. In many places, the term curses and ncurses have been used interchangeably. In reality there is no difference between the two. The ncurses library is only an open-source “clone” of the original unix curses library.
Let’s get started.
The first thing you want to do is to import the package library.
Next, we need to get the object representing the canvas (window) . We will use this windowObject to draw / add other elements to the window.
windowObject = curses.initscr()
With the windowObject initialized, we can add a simple text to be displayed on the window. The height_pos represents the position in the y -axis and the width_pos represents the position in the x-axis. The (0,0) is the top-left corner of the screen.
With the text is place, we would also like to update the screen frequently to reflect any changes. The refresh method provides this functionality.
The screen would exit once it paints the string onto the terminal. The getch() method forces the application to wait for a key press. It can also work as a key handler in applications which perform actions based on certain key press.
In the end, we should allow the terminal to return to its normal state and de-initialize the library.
So that’s all is required to get started with the curses library. The final code looks something like this:
import curses windowObject= curses.initscr() windowObject.addstr(10,20,"That's all Folks!") windowObject.refresh() windowObject.getch() curses.endwin()