'Python while loop in curses module problem

so I'm trying to make an app in the module just to introduce myself to it and I'm having this little problem. It's a menu where you have multiple options to choose. When you choose an option called Functions it should clear the screen and show sort of a sub menu. That works but when I'm trying to navigate in that menu the app just refreshes and goes back to the old menu. I think I know where the problem is, it's just I don't know how to solve it, I'll begin. So I have a class called Functions where are the functions. It looks like this

class Functions:

    global menu
    menu = ['Functions', 'About', 'Exit']
    def print_menu(stdscr, selected_row_idx):
        
        stdscr.clear()
        text = "0xR"
        h, w = stdscr.getmaxyx()

        for idx, row in enumerate(menu):
            x = w//2 - len(row)//2
            y = h//2 - len(menu)//2 + idx
            xLogo = w//2 - len(text)//2
            stdscr.addstr(10, xLogo, text)

            if idx == selected_row_idx:
                stdscr.attron(curses.color_pair(1))
                stdscr.addstr(y,x, row)
                stdscr.attroff(curses.color_pair(1))
            else:
                stdscr.addstr(y,x, row)

        stdscr.refresh()

    def print_about(stdscr):
        stdscr.clear()
        h, w = stdscr.getmaxyx()
        info = ["raven", "blahblahblah"]
        for idx, row in enumerate(info):
            x = w//2 - len(row)//2
            y = h//2 - len(info)//2 + idx
            stdscr.attron(curses.color_pair(2))
            stdscr.addstr(y,x, row)
            stdscr.attroff(curses.color_pair(2))

        stdscr.refresh()

    def funcs(stdscr, functions_current_row_idx):
        stdscr.clear()
        list_of_functions = ['Option1', 'Option2', 'Option3']
        h, w = stdscr.getmaxyx()
        for idx, row in enumerate(list_of_functions):
            x = w//2 - len(row)//2
            y = h//2 - len(list_of_functions)//2 + idx

            if idx == functions_current_row_idx:
                stdscr.attron(curses.color_pair(1))
                stdscr.addstr(y,x, row)
                stdscr.attroff(curses.color_pair(1))
            else:
                stdscr.addstr(y,x, row)

        stdscr.refresh()

the first function called print_menu is the main function that displays the main menu to you it has a list named menu where are the options to choose from then outside of that class I have the main method

def main(stdscr):
    
    curses.curs_set(0)
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
    curses.init_pair(2, curses.COLOR_MAGENTA, curses.COLOR_BLACK)

    current_row_idx = 0
    functions_current_row_idx = 0

    is_in_funcs = False
    while 1:
        is_in_funcs = False
        key = stdscr.getch()
        stdscr.clear()

        
        if is_in_funcs == False:

            if key == curses.KEY_UP and current_row_idx > 0:
                current_row_idx -= 1
            elif key == curses.KEY_DOWN and current_row_idx < len(menu)-1:
                current_row_idx += 1
            elif key == curses.KEY_ENTER or key in [10,13]:
                if current_row_idx == len(menu)-1:
                    sys.exit()
                elif current_row_idx == 1:
                    Functions.print_about(stdscr)
                elif current_row_idx == 0:
                    is_in_funcs = True
                    Functions.funcs(stdscr, functions_current_row_idx)
                stdscr.refresh()
                stdscr.getch()
                
            Functions.print_menu(stdscr, current_row_idx)
        elif is_in_funcs == True:
            if key == curses.KEY_UP and current_row_idx > 0:
                functions_current_row_idx -= 1
            elif key == curses.KEY_DOWN and current_row_idx < len(menu)-1:
                functions_current_row_idx += 1
            elif key == curses.KEY_ENTER or key in [10,13]:
                stdscr.refresh()
                stdscr.getch()
            Functions.funcs(stdscr, functions_current_row_idx)

        stdscr.refresh()

and here goes the problem because in the main() method there is a while loop, everytime you move with your arrow keys, it refreshes changes the row highlight I tried making a boolean called is_in_funcs = False so everytime it checks if the bool is False and if it is it will refresh the original menu

if is_in_funcs == False:

            if key == curses.KEY_UP and current_row_idx > 0:
                current_row_idx -= 1
            elif key == curses.KEY_DOWN and current_row_idx < len(menu)-1:
                current_row_idx += 1
            elif key == curses.KEY_ENTER or key in [10,13]:
                if current_row_idx == len(menu)-1:
                    sys.exit()
                elif current_row_idx == 1:
                    Functions.print_about(stdscr)
                elif current_row_idx == 0:
                    is_in_funcs = True
                    Functions.funcs(stdscr, functions_current_row_idx)
                stdscr.refresh()
                stdscr.getch()

and if you noticed, there is an if statement

elif current_row_idx == 0:
                    is_in_funcs = True
                    Functions.funcs(stdscr, functions_current_row_idx)

when you're on a certain number of the list and hit enter, it takes you to the new menu and it also changes the is_in_funcs to True then it calls the function funcs in Functions class with the parameters then I made new if statement if the is_in_funcs is False

elif is_in_funcs == True:
            if key == curses.KEY_UP and current_row_idx > 0:
                functions_current_row_idx -= 1
            elif key == curses.KEY_DOWN and current_row_idx < len(menu)-1:
                functions_current_row_idx += 1
            elif key == curses.KEY_ENTER or key in [10,13]:
                stdscr.refresh()
                stdscr.getch()
            Functions.funcs(stdscr, functions_current_row_idx)

and I have no idea why it doesn't work. I mean, it's probably because the bool resets and goes back to False, but I don't know how to fix that. I would really appreciate any kind of help ^-^



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source