aegirhall / console-menu

A simple Python menu system for building terminal user interfaces.
MIT License
365 stars 58 forks source link

Question about using FunctionItem.get_return() #64

Open davidva-ecco opened 2 years ago

davidva-ecco commented 2 years ago

I'm having trouble figuring out where/how to call FunctionItem.get_return() so that I can display the return data while a ConsoleMenu is running. Here's an example script to help explain what I'm trying to do.

from consolemenu import *
from consolemenu.items import *

def command_1():
    print("Command 1 ran")
    return "Command 1 ran"

def command_2():
    print("Command 2 ran")
    return "Command 2 ran"

def main():
    menu = ConsoleMenu("Root Menu", "This is the Root Menu Subtitle")

    # Create a menu item that calls a function
    function_item_1 = FunctionItem("Command 1", command_1)
    function_item_2 = FunctionItem("Command 2", command_2)

    menu.append_item(function_item_1)
    menu.append_item(function_item_2)

    # Show the menu
    menu.show()

    print(function_item_1.get_return())
    print(function_item_2.get_return())

if __name__ == "__main__":
    main()

With this approach, the text printed to the screen in command_1() and command_2() gets overwritten immediately when the menu redraws. The get_return() prints after menu.show() don't appear until after exiting the menu, which makes sense since menu.show() is blocking. So I have two questions:

  1. Is there a way, using console-menu, to delay redrawing the menu after a FunctionItem's function is called (maybe until another keyboard/enter input) so that anything printed in the function doesn't get overwritten immediately?

  2. Is there a way to do the same as 1 but using function_item_*.get_return() outside of the functions themselves? I'm assuming I need to use another thread but I'm not sure.

Thanks!