Search code examples
pythonlinuxcurses

multiple Textboxes in curses


I need to create two Textboxes in curses and switch between them when I press Enter

this code put the cursor on the top-left corner of the screen when I press enter for the second Textbox and I can't prevent it.

import curses
import curses.textpad as textpad

def main(screen):
    """screen is a curses screen passed from the wrapper"""

    while True:
        event = screen.getch() 
        if event :
            textpad.Textbox(curses.newwin(1,13,4,0), insert_mode=True).edit()
            textpad.Textbox(curses.newwin(1,13,4,16), insert_mode=True).edit()
            screen.refresh()      

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

Solution

  • Looks like you don't even need to call getch() as this is sufficient:

    import curses
    import curses.textpad as textpad
    
    def main(screen):
        """screen is a curses screen passed from the wrapper"""
        while True:
            textpad.Textbox(curses.newwin(1,13,4,0), insert_mode=True).edit()
            textpad.Textbox(curses.newwin(1,13,4,16), insert_mode=True).edit()
    
    if __name__ == '__main__':   
        curses.wrapper(main)