1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Malfunctioning Keybinding with python prompt_toolkit package

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 13:42.

  1. Stack

    Stack Membro Participativo

    I have this block of code in my python CLI app:

    from prompt_toolkit.key_binding import KeyBindings
    from prompt_toolkit.keys import Keys
    from prompt_toolkit.application import run_in_terminal
    #Other irrelevent code
    binding = KeyBindings()
    @binding.add('c-l')
    def _(event):
    clear_screen()
    @binding.add('c-x')
    def _(event):
    def save():
    save = confirm("Wanna save the current state: ",suffix="[y/N]" )
    if save:
    print(to_do_list.save_current_state(json_data_file))
    else:
    print('Data not saved.')
    event.app.exit()
    run_in_terminal(save)


    The Ctrl+l keybinding works flawlessly yet thr Ctrl+x one won't work.
    Any help would be much appreciated.

    I searched and ran throught the documentation yet it wasn't much help.
    I tried this fix provided by claude AI:

    from prompt_toolkit.application import get_app

    @binding.add('c-x')
    def _(event):
    app = get_app()

    def save():
    save_confirmed = confirm("Do you want to save the current state? ", suffix="[y/N] ")
    if save_confirmed:
    print(to_do_list.save_current_state(json_data_file))
    else:
    print('Data not saved.')
    app.exit()

    app.loop.call_soon_threadsafe(save)


    But didn't work.

    Continue reading...

Compartilhe esta Página