pythontextboxpython-curses

Add default text to curses Textbox object


I'm using the curses python library in an application and have implemented a Textbox as well https://docs.python.org/3/library/curses.html#textbox-objects.

Is there a way to add a default text when displaying the text box that can then be either edited or replaced by the user?

Example of textbox usage:

import curses
import curses.textpad

def main(stdscr):
    stdscr.addstr(1, 0, "Text: ")
    stdscr.refresh()
    
    win = curses.newwin(1, 20, 1, 6)
    textbox = curses.textpad.Textbox(win)

    # add a default text here to be displayed
    # which can be modified, deleted or replaced
    text = textbox.edit()
    
    stdscr.addstr(3, 0, "Input: ")
    stdscr.addstr(3, 7, text)
    stdscr.refresh()

    stdscr.getch()

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

Solution

  • not 100% convinced your example does what you expect, here's a rudimentary example that prompts and captures the input.

    import curses
    
    def main(stdscr):
    
        stdscr.clear()
    
        stdscr.addstr(10, 10, "Your answer here : ")
    
        curses.echo() # so we can see what's being entered
        user_input = stdscr.getstr(10, 30).decode('utf-8')
    
        stdscr.addstr(12, 10, f"You entered: {user_input}")
    
        stdscr.addstr(14, 10, f"press a key to quit:")
        stdscr.getch()
    
    # init
    curses.wrapper(main)
    
    

    hope this helps