pythonnicegui

Get the selected date from python/nicegui


I have two functions for getting date from user, which are:

def date_selector():
    with ui.input('Date') as date:
        with date.add_slot('append'):
            ui.icon('edit_calendar').on('click', lambda: menu.open()).classes('cursor-pointer')
    with ui.menu() as menu:
        ui.date().bind_value(date)
    s_date = date.value
    print(s_date)
    return s_date
def tab_date():
    ui.label(date_selector())
    return

But it is not assign value to s_date. How can I make?


Solution

  • You're creating a ui.date element and immediately read its value. But the user had no chance to set any value, yet.

    Instead you should react on the on_change event, e.g. like this:

    def handle_date(event):
        ui.label(event.value)
        menu.close()
    
    
    with ui.input('Date', on_change=handle_date) as date:
        with date.add_slot('append'):
            ui.icon('edit_calendar').on('click', lambda: menu.open()).classes('cursor-pointer')
    with ui.menu() as menu:
        ui.date().bind_value(date)