Npyscreen with Python: Beginner

Vera Worri
Vera Worri
Published in
2 min readDec 4, 2016

I’m writing while I am learning how to work with npyscreen. I use the Pycharm IDE and my version of python is 2.7.12 on a macbook.

I downloaded npyscreen in the terminal using the command

  • sudo easy_install npyscreen

Afterwards I just copied and pasted a random code I found online to test if/how it would work. Doing this returned an error [ _curses.error: setupterm: could not find terminal] into the Pycharm terminal so I did some research and found here: https://groups.google.com/forum/#!topic/npyscreen/Sf6MxZ5nlL0

that pycharm terminal can be difficult so I had to use my computer terminal. I typed

  • sudo python

into the terminal and dragged and dropped the file into the terminal box (because the file is very nested). The file worked beautifully

So now I have to adapt it to my code. This code is a random quote machine that I built. It is a simple UI that displays the quote and has a button to update the quote that is show.

I started out trying to find the right class for my UI. That turned out to be the ActionForm class. It has a cancel and ok button built in so I have something to work with. I do not know how to make buttons yet. So…

I started with a create function and added two elements one as a title and the other for the output.

def create(self):
self.add(npyscreen.TitleText, name = "Text:", value= "Random Quote Machine")
self.quote = self.add(npyscreen.TextfieldUnicode, name = "Output", value = RandomQuoteMachine())

Then, because I cannot make buttons, I reprogrammed the OK button to reload the self.quote element and therefore get other quote from the Random Quote Machine.

def on_ok(self):
self.quote.value = RandomQuoteMachine()

The documentation for Npyscreen is a bit confusing if you are like me and need to see example code. I did a lot of research to inch along the learning curve but youTube was a real help especially this video.

I hope this helps some one out there who is trying to learn.

--

--