Python:函数内部的全局变量+诅咒

时间:2011-03-01 18:28:20

标签: python curses

我非常非常困惑......

基本上尝试声明一个指向curses窗口的全局变量,这样我就可以使用一个调试命令,但它会抱怨AttributeError: 'NoneType' object has no attribute 'addstr'这意味着它没有被设置?请帮忙!

wDebug = None

def start(stdscr):
    curses.nocbreak()
    curses.echo()
    screenSize = stdscr.getmaxyx()

    wDebug = curses.newwin(5, screenSize[1], 0, 0);

    curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)

    wDebug.bkgd(curses.color_pair(1))
    wDebug.refresh()

    /* Snip */

    awaitInput(wInput)

    while 1: pass

def awaitInput(window): 
  while 1:
    msg = /* Snip */
    sendMessage(msg)

def sendMessage(msg):
  /* Snip */
  debug("Send message")

def debug(msg):
  wDebug.addstr(msg + "\n")
  wDebug.refresh()  

非常感谢你的时间,

1 个答案:

答案 0 :(得分:1)

您需要使用global statement

wDebug = None

def start(stdscr):
    global wDebug
    #...
    wDebug = curses.newwin(5, screenSize[1], 0, 0);

来自文档:

  

如果没有global

,则无法分配全局变量