Question

I am attempting to make menu system using the curses module. I have the following code:

import sys, os, traceback, curses

def main(scrn):
    screen = scrn
    screen.border(0)
    curses.start_color()
    curses.init_pair(2,curses.COLOR_WHITE,curses.COLOR_BLUE)

    while True:
        event = screen.getch()
        if event == ord("q"):
            break
        elif event == curses.KEY_RIGHT:
            #enter function containing while loop, passing job
            job_sub()


#loop to hand the subscreen for a job element
def job_sub():

    screen = curses.newwin(5, 10, 3, 3)
    screen.box()
    objects =["RUN", "MAINTAIN", "EDIT"]

    for i in range( len(objects) ):
        if i == 0:
            screen.addstr(i+1,1, objects[i], curses.color_pair(2))
        else:
            screen.addstr(i+1,1, objects[i])

    screen.refresh()

    while True:
        event = screen.getch()
        if event == curses.KEY_LEFT:
            break

    screen.erase()
    return

if __name__=='__main__':
    try:
        # Initialize curses
        screen=curses.initscr()
        curses.noecho()
        curses.cbreak()
        screen.keypad(1)

        main(screen) 

        screen.keypad(0)
        curses.echo()
        curses.nocbreak()
        curses.endwin()
    except:
        # In event of error, restore terminal to sane state.
        screen.keypad(0)
        curses.echo()
        curses.nocbreak()
        curses.endwin()
        traceback.print_exc()

The program runs until I hit the right arrow key. After that, it freezes, like it's stuck in a loop. It won't respond to any more input. Any help is appreciated.

Was it helpful?

Solution

In your job_sub() function you create a new window but you don't enable the keypad for it. As a result the arrow key is not sending a curses.KEY_LEFT value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top