Search code examples
pythontextcurses

(python) How to create static text in curses


I am creating a text adventure. How could I to add static text to this. What i mean is some text that always stays on the left side of the window. Even if all the other text is scrolling down. Also how could i make this text red.


Solution

  • Here is an example that shows some static text in red(that always stays on top):

    import sys
    import curses
    
    
    curses.initscr()
    
    if not curses.has_colors():
        curses.endwin()
        print "no colors"
        sys.exit()
    else:
        curses.start_color()
    
    curses.noecho()    # don't echo the keys on the screen
    curses.cbreak()    # don't wait enter for input
    curses.curs_set(0) # don't show cursor.
    
    RED_TEXT = 1
    curses.init_pair(RED_TEXT, curses.COLOR_RED, curses.COLOR_BLACK)
    
    window = curses.newwin(20, 20, 0, 0)
    window.box()
    staticwin = curses.newwin(5, 10, 1, 1)
    staticwin.box()
    
    staticwin.addstr(1, 1, "test", curses.color_pair(RED_TEXT))
    
    cur_x = 10
    cur_y = 10
    while True:
        window.addch(cur_y, cur_x, '@')
        window.refresh()
        staticwin.box()
        staticwin.refresh()
        inchar = window.getch()
        window.addch(cur_y, cur_x, ' ')
        # W,A,S,D used to move around the @
        if inchar == ord('w'):
            cur_y -= 1
        elif inchar == ord('a'):
            cur_x -= 1
        elif inchar == ord('d'):
            cur_x += 1
        elif inchar == ord('s'):
            cur_y += 1
        elif inchar == ord('q'):
            break
    curses.endwin()
    

    A screenshot of the result:

    enter image description here

    Remember that windows on top must be refresh()ed last otherwise the windows that should go below are drawn over them.

    If you want to change the static text do:

    staticwin.clear()   #clean the window
    staticwin.addstr(1, 1, "insert-text-here", curses.color_pair(RED_TEXT))
    staticwin.box()     #re-draw the box
    staticwin.refresh()
    

    Where the 1, 1 means to start writing from the second character of the second line(remember: coordinates start at 0). This is needed since the window's box is drawn on the first line and first column.