Python: Global Variables From Within Functions + Curses
I am being very, very confused...
Basically trying to declare a global variable pointing to a curses window so I can use a debug command how开发者_开发知识库ever it complains AttributeError: 'NoneType' object has no attribute 'addstr'
which implies it is not being set? Please help!
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()
Many thanks for your time,
You need to use a global statement:
wDebug = None
def start(stdscr):
global wDebug
#...
wDebug = curses.newwin(5, screenSize[1], 0, 0);
From the documentation:
It would be impossible to assign to a global variable without
global
精彩评论