Skip to content

Retrieve the button that the user clicked - Func activated_args #55

Description

@elieworkspace

Goal : Retrieve the button that the user clicked

Error : e.user_input is not callable

Before :

def activated_args(_, event):
    global result
    e = ToastActivatedEventArgs._from(event)
    user_input = dict([(name, IPropertyValue._from(
        e.user_input[name]).get_string()) for name in e.user_input()])
    result = {
        'arguments': e.arguments,
        'user_input': user_input
    }
    return result

from win11toast import toast

def _on_click(args):
    button_clicked = args.get('arguments')
    print(f"Clicked : {button_clicked}")
    
    if args.get('user_input'):
        print(f"User Input : {args['user_input']}")


buttons = ["Approve", "Dismiss", "Other"]

a = toast(
    "Notification",
    "Which button are you going to click?",
    buttons=buttons,
    on_click=_on_click,
    scenario='incomingCall'
)

Error :

Traceback (most recent call last):
  File "C:\Users\\\envs\\Lib\site-packages\win11toast.py", line 366, in <lambda>
    activated_future.set_result, on_click(activated_args(*args))
  File "C:\Users\\\envs\\Lib\site-packages\win11toast.py", line 169, in activated_args
    e.user_input[name]).get_string()) for name in e.user_input()])
TypeError: 'winrt._winrt_windows_foundation_collections.ValueSet' object is not callabl

After :

def activated_args(_, event):
    global result
    e = ToastActivatedEventArgs._from(event)
    
    # Correction: e.user_input is not callable
    user_input = dict([(name, IPropertyValue._from(e.user_input[name]).get_string()) for name in e.user_input])
    
    result = {
        'arguments': e.arguments,
        'user_input': user_input
    }
    return result


from lib.win11toast import toast

result = toast(
    "Notification",
    "Which button are you going to click?",
    buttons=["Approve", "Dismiss", "Other"]
)

# If the result is a future, wait for it to complete
if hasattr(result, "result"):
    result = result.result()

# Display which button was clicked
print(f"Button clicked: {result['arguments']}")

Output :

{'arguments': 'http:Approve', 'user_input': {}}
Button clicked: http:Approve

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions