Skip to content Skip to sidebar Skip to footer

Callbacks With Python Curses

I have code that looks like this: stdscr.nodelay(1) while True: c = stdscr.getch() if c != -1: stdscr.addstr('%s was pressed\n' % c) if time() - last > 1:

Solution 1:

You could try Urwid instead.

Urwid provides a higher-level toolkit on top of curses and includes an event loop to handle keyboard and mouse input. It either uses it's own select-based event loop, or can hook into gevent or Twisted.

In addition to handling keyboard input efficiently you'll also have a host of options to handle user input with edit boxes, list controls and more.

Solution 2:

How about using half-delay mode to make getch() blocking?

import time
import curses

stdscr = curses.initscr()
curses.halfdelay(10) # 10/10 = 1[s] intevaltry:
    whileTrue:
        c = stdscr.getch()
        if c != -1:
            stdscr.addstr("%s was pressed\n" % c)
        stdscr.addstr("time() == %s\n" % time.time())
finally:
    curses.endwin()

Post a Comment for "Callbacks With Python Curses"