How can I escape sub-window with or without any input string in python curses?

185 views Asked by At

In python curses, if I draw a sub-window using scr.subwin() to get input with getstr() function as below

search_box = screen.subwin(3, 30, 20, 30)
search_box.clear()
search_box.box()
search_box.addstr(1, 1, "GoTo:")
search_box.refresh()
curses.echo()
new_x = int(search_box.getstr(1, 6, 20))
search_box.clear()

and I decide not provide any input. How can I escape new sub-window?

Thanks

1

There are 1 answers

0
Manu On

I have resolved my problem as following

search_box = screen.subwin(3, 30, 20, 30)
search_box.clear()
search_box.box()
in_str = "GoTo:"
val_str = ""
search_box.addstr(1, 1, in_str)
search_box.refresh()
curses.echo()
char = search_box.getch()
try:
    int(chr(char))
    val_str += chr(char)
except:
    pass

while  char != 27: # Escape Key Value
    if char == 10: # Enter Key Value
        new_x = int(val_str)
        break
    else:
        search_box.clear()
        search_box.box()
        search_box.addstr(1, 1, in_str+val_str)
        search_box.refresh()
        char = search_box.getch()
        try:
            int(chr(char))
            val_str += chr(char)
        except:
            if char == 127:
                if len(val_str) > 0:
                    val_str = val_str[:-1]