prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.11k stars 717 forks source link

How to link RadioList contents (i.e. RadioList change event) #1777

Open vlovich opened 10 months ago

vlovich commented 10 months ago

I want to create 2 RadioLists where the contents of the 2nd radio list are determined by the selection in the first radio list. If I'm not mistaken, RadioList doesn't have any hook to let you monitor that the event changed. Wondering how I can accomplish this.

e.g.

( * ) Choice 1   | ( * ) Option 1
(   ) Choice 2   | (   ) Option 2

and

(   ) Choice 1   | ( * ) Option 3
( * ) Choice 2   | (   ) Option 4
stripuramallu3 commented 8 months ago

RadioList has a _handle_enter function that changes the selection when a radio list item is changed. I wrote my own handle_enter and set it to the _handle_enter:

radio_list = RadioList(values=radio_values)
def handle_enter():
    new_value = radio_list.values[self.radios._selected_index][0]
    radio_list.current_value = new_value
    # other logic here
radio_list._handle_enter = handle_enter