Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
nodelay()导致python诅咒程序退出_Python_Ncurses_Curses - Fatal编程技术网

nodelay()导致python诅咒程序退出

nodelay()导致python诅咒程序退出,python,ncurses,curses,Python,Ncurses,Curses,我已经用python编写了一个诅咒程序。它运行良好。但是,当我使用nodelay()时,程序在终端中启动后立即退出,没有显示任何内容(只是一个新的提示) 编辑 此代码将复制错误: sc = curses.initscr() sc.nodelay(1) # But removing this line allows the program to run properly for angry in range(20): sc.addstr(angry, 1, "hi") sc=c

我已经用python编写了一个诅咒程序。它运行良好。但是,当我使用
nodelay()
时,程序在终端中启动后立即退出,没有显示任何内容(只是一个新的提示)

编辑

此代码将复制错误:

sc = curses.initscr() sc.nodelay(1) # But removing this line allows the program to run properly for angry in range(20): sc.addstr(angry, 1, "hi") sc=curses.initscr() sc.nodelay(1)#但是删除这一行可以使程序正常运行 对于范围(20)内的愤怒: sc.addstr(愤怒,1,“嗨”) 这是我的全部代码

import curses, time, sys, random def paint(x, y, i): #... def string(s, y): #... def feed(): #... sc = curses.initscr() curses.start_color() curses.curs_set(0) sc.nodelay(1) ######################################### # vars + colors inited for angry in range(20): try: dir = chr(sc.getch()) sc.clear() feed() #lots of ifs body.append([x, y]) body.pop(0) for point in body: paint(*point, i=2) sc.move(height-1, 1) sc.refresh() time.sleep(wait) except Exception as e: print sys.exc_info()[0], e sc.getch() curses.beep() curses.endwin() 导入诅咒、时间、系统、随机 def涂料(x、y、i): #... def字符串(s,y): #... def提要(): #... sc=curses.initscr() 诅咒。开始使用颜色() 诅咒。诅咒集(0) 理学士诺德雷(1)######################################### #变量+颜色初始化 对于范围(20)内的愤怒: 尝试: dir=chr(sc.getch()) sc.clear() 提要() #很多如果 body.append([x,y]) body.pop(0) 对于主体中的点: 绘制(*点,i=2) sc.移动(高度-1,1) sc.刷新() 时间。睡眠(等待) 例外情况除外,如e: 打印系统exc_info()[0],e sc.getch() 诅咒 诅咒
为什么会发生这种情况,我如何才能安全地使用
nodelay()

在运行小型测试程序时,无论是否使用
sc.nodelay()
行,我都看不出有什么不同


这两个案例都不会在屏幕上打印任何内容…

我已经重写了您的小型演示,以使基本功能正常工作。它有一个非阻塞getch()。如果在调用getch()时按住Q键,程序将结束,否则循环将继续

import curses, time

def main(sc):
    sc.nodelay(1)

    for angry in range(20):
        sc.addstr(angry, 1, "hi")
        sc.refresh()

        if sc.getch() == ord('q'):
            break

        time.sleep(1)

if __name__=='__main__':
    curses.wrapper(main)
我所做的最重要的更改是使用获取屏幕上下文,而不是使用curses.initscr()。好处是,如果您的程序遇到未捕获的异常(例如,命中^C),它将撤消您对终端所做的所有更改,如在退出之前禁用光标。在调试时,它会有很大帮助


从这里开始,我建议您以非常小的步骤重新添加程序的功能。诅咒是一种痛苦的工作,如果你一下子做了很多改变,很难找出是哪一个导致了事情的破裂。祝你好运

尝试将代码缩减到复制错误所需的最小值。例如,您可以设置nodelay(),然后进入一个无限循环,当您获取一个键时,该循环会中断并退出。如果bug在最短的测试用例中消失了,请找出发生了什么变化。如果这对发布失败的测试用例没有帮助的话。@Philip good thinking,已经这样做了,并发布了结果。问题是,对于我来说,使用
sc.nodelay()
,诅咒窗口甚至没有打开,因此我什么都不能做,因为没有什么可以做的。添加主包装器使我能够看到,当
getch()
没有返回任何内容(因为没有可用的输入)并试图放入
chr()
时,由于出现错误,我正在退出。感谢您的帮助,这将在解决其他bug时非常有用。