def askString(message, length=40): """Asks the user to enter a string. message: the window title. length: expected maximum length for the string. Returns the inserted string or None if the user canceled the input.""" retval = None winY = screenH / 2 - 2 winWidth = max(len(message), length) + 4 winX = (screenW - winWidth) / 2 panel = CursesPanel(3, winWidth, winY, winX, title=message) panel.focus() window = panel._window subWin = window.derwin(1, length, 1, 1) subWin.keypad(1) textBox = curses.textpad.Textbox(subWin) textBox.stripspaces = True curses.panel.update_panels() curses.doupdate() done = False while not done: c = subWin.getch() if c == curses.ascii.NL: retval = textBox.gather() done = True elif c == curses.ascii.ESC: done = True # so that we return None else: textBox.do_command(c) del textBox del subWin del window return retval
def errorMessage(message): """Displays an error message.""" winY = screenH / 2 - 2 winX = (screenW - len(message)) / 2 + 2 winWidth = len(message) + 4 if len(message) < 5: winWidth = 9 panel = CursesPanel(5, winWidth, winY, winX, "Error") panel.focus() panel._window.addstr(2, 2, message) panel.getch() del panel