Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why does this always show an error at the curses.wrapper(main) line? Please someone fix this!

–1 vote
asked Mar 17, 2021 by Костадин Клемов (330 points)
import curses
import random

def menu(root, current_row):
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
    
    height, width = root.getmaxyx()
    
    menu = ["Option 1", "Option 2", "Option 3", "Option 4"]
    
    for idx, element in enumerate(menu):
        y = height // 5 * idx
        x = width // 5
        
        if idx == current_row:
            root.attron(curses.color_pair(1))
            root.addstr(y, x, element)
            root.attroff(curses.color_pair)
            
        else:
            root.addstr(y, x, element)
        
        root.addstr(y, x, element)
        
    root.refresh()
    
def main(root):
    curses.curs_set(0)
    current_row = 0
    
    menu(root, current_row)

    while True:
        key = root.getch()
        
        if key == curses.KEY_UP and current_row > 0:
            current_row -= 1
        
        elif key == curses.KEY_DOWN and current_row > 3:
            current_row += 1
        
        elif key == ord("e"):
            break
        
        elif key == ord("q"):
            break
        
        menu(root, current_row)
    
    root.refresh(main)
    
curses.wrapper()

1 Answer

0 votes
answered Mar 17, 2021 by xDELLx (10,500 points)

The error ,for me ,shows which file was complaining.

Checking file /usr/lib/python3.8/curses/__init__.py shows the wrapper method needs a function which will be triggered by curses on startup. I added "main" as parameter & it went fine from there.

Changes --->  curses.wrapper(main)

Also changed line 18 from shown below

 18             #root.attroff(curses.color_pair)
 19             root.attroff(0)
commented Mar 18, 2021 by Костадин Клемов (330 points)
can you please send me the whole code?
commented Mar 19, 2021 by xDELLx (10,500 points)
import curses
import random

def menu(root, current_row):
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
    
    height, width = root.getmaxyx()
    
    menu = ["Option 1", "Option 2", "Option 3", "Option 4"]
    
    for idx, element in enumerate(menu):
        y = height // 5 * idx
        x = width // 5
        
        if idx == current_row:
            root.attron(curses.color_pair(1))
            root.addstr(y, x, element)
            #root.attroff(curses.color_pair)
            root.attroff(0)
            
        else:
            root.addstr(y, x, element)
        
        root.addstr(y, x, element)
        
    root.refresh()
    
def main(root):
    curses.curs_set(0)
    current_row = 0
    
    menu(root, current_row)

    while True:
        key = root.getch()
        
        if key == curses.KEY_UP and current_row > 0:
            current_row -= 1
        
        elif key == curses.KEY_DOWN and current_row > 3:
            current_row += 1
        
        elif key == ord("e"):
            break
        
        elif key == ord("q"):
            break
        
        menu(root, current_row)
    
    root.refresh(main)
    
curses.wrapper(main)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...