简体   繁体   English

Setupterm在使用curses的Python程序中找不到终端

[英]Setupterm could not find terminal, in Python program using curses

I am trying to get a simple curses script to run using Python (with PyCharm 2.0). 我试图获得一个简单的curses脚本以使用Python(与PyCharm 2.0)一起运行。

This is my script: 这是我的脚本:

import curses
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
while 1:
    c = stdscr.getch()
    if c == ord('p'): print("I pressed p")
    elif c == ord('q'): break

curses.nocbreak(); stdscr.keypad(0); curses.echo()
curses.endwin()

When I run this from my IDE (PyCharm 2) I get the following error: 当我从IDE(PyCharm 2)运行此程序时,出现以下错误:


_curses.error: setupterm: could not find terminal
Process finished with exit code 1

If I run the script from bash it will simply be stuck in the while loop not reacting to either pressing p or q. 如果我从bash运行脚本,它将仅停留在while循环中,而不会对按p或q做出反应。

Any help would be appreciated. 任何帮助,将不胜感激。

You must set enviroment variables TERM and TERMINFO , like this: 您必须像这样设置环境变量TERMTERMINFO

export TERM=linux
export TERMINFO=/etc/terminfo

And, if you device have no this dir ( /etc/terminfo ), make it, and copy terminfo database. 并且,如果您的设备没有此目录( /etc/terminfo ),请制作该目录并复制terminfo数据库。

For "linux", and "pcansi" terminals you can download database: 对于“ linux”和“ pcansi”终端,您可以下载数据库:

Go to run/debug configuration(the one next to Pycharm run button). 转到运行/调试配置(Pycharm运行按钮旁边的一个)。 Sticking on Emulate Terminal In Output Console. 在输出控制台中粘贴在仿真终端上。 Then you will be able to run your program with the run button. 然后,您将能够通过运行按钮运行程序。

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. 我还添加了try:finally:即使循环中发生了一些简单的错误,也要确保将终端恢复为原始外观。

You have to use the addstr to make sure the text is going to be displayed inside the window. 您必须使用addstr来确保文本将在窗口内显示。

I was having the same problem. 我遇到了同样的问题。 See Curses Programming with Python - Starting and ending a curses application . 请参阅使用Python进行Curses编程-启动和结束curses应用程序

There's a curses.wrapper() function that simplifies the process of starting/ending a curses application. 有一个curses.wrapper()函数可简化启动/结束curses应用程序的过程。

Here's the example from the Python doc: 这是Python文档中的示例:

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)

If you are using macOS and running PyCharm you will have to set environment variables from the IDE itself, for execution scope. 如果您正在使用macOS并运行PyCharm,则必须从IDE本身设置环境变量以实现执行范围。

Edit Configurations -> Environment variables 编辑配置->环境变量

then add the below name-value pairs 然后添加以下名称/值对

TERM linux TERM Linux

TERMINFO /etc/zsh 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. TERM的默认值为xterm,其他值为[konsole,rxvt]例如,rxvt通常支持16种颜色。 You can try to set TERM to rxvt-16color. 您可以尝试将TERM设置为rxvt-16color。

/bin/zsh is path of the terminal application that I use in mac. / bin / zsh是我在Mac中使用的终端应用程序的路径。

It's like telling your program that you will be logging into linux(TERM) like terminal which can be found at /bin/zsh. 这就像告诉您的程序,您将要登录到linux(TERM)这样的终端,该终端可以在/ bin / zsh中找到。 For using bash shell it could be something like /bin/bash . 使用bash shell可能类似于/ bin / bash。

You'll see this error if you're using Idle. 如果您使用的是空闲状态,则会看到此错误。 It's because of Idle's default redirection of input/output. 这是由于Idle的默认输入/输出重定向。 Try running your program from the command line. 尝试从命令行运行程序。 python3 <filename>.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM