2016-04-03 13 views
4

Ich fange an, Flüche in Python zu lernen. Ich benutze Python 3.5 auf Mac. Wenn ich versuche, mit dem folgenden Fehler in dem linken unteren Ecke Programmabsturz zu schreiben:Flüche rufen Addch auf der unteren rechten Ecke

$ python ex_curses.py 
[...] 
    File "ex_curses.py", line 19, in do_curses 
    screen.addch(mlines, mcols, 'c') 
    _curses.error: add_wch() returned ERR 

Das Beispielprogramm ist:

import curses 

def do_curses(screen): 
    curses.noecho() 
    curses.curs_set(0) 
    screen.keypad(1) 

    (line, col) = 12, 0 
    screen.addstr(line, col, "Hello world!") 
    line += 1 
    screen.addstr(line, col, "Hello world!", curses.A_REVERSE) 

    screen.addch(0, 0, "c") 

    (mlines, mcols) = screen.getmaxyx() 
    mlines -= 1 
    mcols -= 1 
    screen.addch(mlines, mcols, 'c') 

    while True: 
     event = screen.getch() 
     if event == ord("q"): 
      break 
    curses.endwin() 

if __name__ == "__main__": 
    curses.wrapper(do_curses) 

mir das Gefühl, dass ich etwas offensichtlich haben fehle. Aber ich weiß nicht was.

Antwort

1

Das ist das erwartete Verhalten (eine Eigenart), weil addch versucht, wrap in die nächste Zeile nach dem Hinzufügen eines Zeichens. Es gibt einen comment in lib_addch.c Umgang mit diesem:

/* 
* The _WRAPPED flag is useful only for telling an application that we've just 
* wrapped the cursor. We don't do anything with this flag except set it when 
* wrapping, and clear it whenever we move the cursor. If we try to wrap at 
* the lower-right corner of a window, we cannot move the cursor (since that 
* wouldn't be legal). So we return an error (which is what SVr4 does). 
* Unlike SVr4, we can successfully add a character to the lower-right corner 
* (Solaris 2.6 does this also, however). 
*/ 
1

Für die Zukunft Leser. Nachdem die @Thomas Dickey Antwort gegeben habe, habe ich den folgenden Code zu meinem Code hinzugefügt.

try: 
    screen.addch(mlines, mcols, 'c') 
except _curses.error as e: 
    pass 

Jetzt ist mein Code wie folgt aussieht:

import curses 
import _curses 

def do_curses(screen): 
    curses.noecho() 
    curses.curs_set(0) 
    screen.keypad(1) 

    (line, col) = 12, 0 
    screen.addstr(line, col, "Hello world!") 
    line += 1 
    screen.addstr(line, col, "Hello world!", curses.A_REVERSE) 

    screen.addch(0, 0, "c") 

    (mlines, mcols) = screen.getmaxyx() 
    mlines -= 1 
    mcols -= 1 
    try: 
     screen.addch(mlines, mcols, 'c') 
    except _curses.error as e: 
     pass 

    while True: 
     event = screen.getch() 
     if event == ord("q"): 
      break 
    curses.endwin() 

if __name__ == "__main__": 
    curses.wrapper(do_curses) 
Verwandte Themen