Setupterm could not find terminal, in Python program using curses

前端 未结 6 1570
礼貌的吻别
礼貌的吻别 2020-11-28 13:07

I am trying to get a simple curses script to run using Python (with PyCharm 2.0).

This is my script:

import curses
stdscr = curses.initscr()
curses.n         


        
相关标签:
6条回答
  • 2020-11-28 13:42

    You'll see this error if you're using Idle. It's because of Idle's default redirection of input/output. Try running your program from the command line. python3 <filename>.py

    0 讨论(0)
  • 2020-11-28 13:49

    You must set enviroment variables TERM and TERMINFO, like this:

    export TERM=linux
    export TERMINFO=/etc/terminfo
    

    And, if you device have no this dir (/etc/terminfo), make it, and copy terminfo database.

    For "linux", and "pcansi" terminals you can download database:

    • http://forum.xda-developers.com/attachment.php?attachmentid=2134052&d=1374459598
    • http://forum.xda-developers.com/showthread.php?t=552287&page=4
    0 讨论(0)
  • 2020-11-28 13:49

    If you are using macOS and running PyCharm you will have to set environment variables from the IDE itself, for execution scope.

    Edit Configurations -> Environment variables

    then add the below name-value pairs

    TERM linux

    TERMINFO /etc/zsh

    The above is equivalent to exporting environment variable from the console which is done like this

    $ export TERM=linux
    $ export TERMINFO=/bin/zsh
    

    the default for TERM is xterm, other values are [konsole, rxvt] rxvt for example is often built with support for 16 colors. You can try to set TERM to rxvt-16color.

    /bin/zsh is path of the terminal application that I use in mac.

    It's like telling your program that you will be logging into linux(TERM) like terminal which can be found at /bin/zsh. For using bash shell it could be something like /bin/bash .

    0 讨论(0)
  • 2020-11-28 13:52

    I found this question when searching for examples because I am also learning to use curses so I don't know much about it. I know this works though:

    import curses
    try:
        stdscr = curses.initscr()
        curses.noecho()
        curses.cbreak()
        stdscr.keypad(1)
        while 1:
            c = stdscr.getch()
            if c == ord('p'):
                stdscr.addstr("I pressed p")
            elif c == ord('q'): break
    finally:
        curses.nocbreak(); stdscr.keypad(0); curses.echo()
        curses.endwin()
    

    I also added the try: finally: to make sure I get the terminal to it's original appearance even if something simple goes wrong inside the loop.

    You have to use the addstr to make sure the text is going to be displayed inside the window.

    0 讨论(0)
  • 2020-11-28 13:57

    Go to run/debug configuration(the one next to Pycharm run button). Sticking on Emulate Terminal In Output Console. Then you will be able to run your program with the run button.

    0 讨论(0)
  • 2020-11-28 14:03

    I was having the same problem. See Curses Programming with Python - Starting and ending a curses application.

    There's a curses.wrapper() function that simplifies the process of starting/ending a curses application.

    Here's the example from the Python doc:

    from curses import wrapper
    
    def main(stdscr):
        # Clear screen
        stdscr.clear()
    
        # This raises ZeroDivisionError when i == 10.
        for i in range(0, 11):
            v = i-10
            stdscr.addstr(i, 0, '10 divided by {} is {}'.format(v, 10/v))
    
        stdscr.refresh()
        stdscr.getkey()
    
    wrapper(main)
    
    0 讨论(0)
提交回复
热议问题