The Python Oracle

Python curses how to change cursor position with curses.setsyx(y,x)?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Dreamlands

--

Chapters
00:00 Python Curses How To Change Cursor Position With Curses.Setsyx(Y,X)?
01:02 Accepted Answer Score 3
01:20 Answer 2 Score 6
01:29 Answer 3 Score 0
02:13 Thank you

--

Full question
https://stackoverflow.com/questions/5150...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #curses

#avk47



ANSWER 1

Score 6


you can use stdscr.getyx() and stdscr.move(y, x) instead.




ACCEPTED ANSWER

Score 3


setsyx and getsyx affect a special workspace-screen named newscr, which is used to construct changes sent to stdscr when you call refresh.

stdscr has its own position, which is not affected by those calls.




ANSWER 3

Score 0


stdscr.move(y, x) moves the stdscr’s cursor, which is used for output position of characters. (so you should use this one)

curses.setsyx(y,x) moves the newscr's cursor which is used for show the cursor on the screen.

How to use setsyx():

for i in range(10):
    # output i
    stdscr.addstr(f"{i} ")
    # refresh stdscr to newscr(not show on the screen)
    stdscr.noutrefresh()
    # move the cursor of newscr to (1,0)
    curses.setsyx(1,0)
    # show it 
    curses.doupdate()
    time.sleep(1)
    
stdscr.getch()

You will see the Number showed one by one, but the cursor always show at (1,0)

enter image description here

The normal refresh() call is simply noutrefresh() followed by doupdate(); but we need setsyx() in the middle, so call them separately.