Search code examples
pythonpython-3.4cursescpythonpython-curses

How do I fix a converting to parameter 2 error in python curses?


I was working in Unicurses, a cross platform module of curses for python. I was trying to put the '@' character in the center of my console. My code was this:

from unicurses import *
def main():
    stdscr = initscr()
    max_y, max_x = getmaxyx( stdscr )
    move( max_y/2, max_x/2 )
    addstr("@")
    #addstr(str(getmaxyx(stdscr)))
    getch()
    endwin()
    return 0
if __name__ == "__main__" :
    main()

I kept getting the error

ctypes.ArgumentError was unhandled by user code
Message: argument 2: <class 'TypeError'>: Don't know how to convert parameter 2

for this line:

move( max_y/2, max_x/2 )

Does anyone know the cause and fix for this error. Thanks!


Solution

  • The problem is that you're passing floats to the move function, while you should pass integers. Use the integer division operator // instead of /.

    from unicurses import *
    def main():
        stdscr = initscr()
        max_y, max_x = getmaxyx( stdscr )
        move( max_y//2, max_x//2 ) # Use integer division to truncate the floats
        addstr("@")
        getch()
        endwin()
        return 0
    if __name__ == "__main__" :
        main()