pythonenaml

Enaml Get Arguments Passed in an Event Handler


I'm designing a UI with Enaml in Python. I have a custom control containing , say two buttons. Each time any of the two buttons is clicked, one is 1 and the other id-ed 2, I want the parent container has a sense which one is clicked. So the event handler from parent accept an extra parameter distinguishing the source of the event. Here is my code

from enaml.widgets.api import (
    Window, Container, PushButton
)

enamldef TwoButtons(Container):  
    attr cont
    PushButton:        
        text = 'Button1'
        clicked :: cont.clicked(1)

    PushButton:
        text = 'Button2'
        clicked :: cont.clicked(2)


enamldef Main(Window):
    Container:
        attr buttonId
        event clicked

        TwoButtons:
            cont = parent

        clicked ::
            # A way to read the event handler argument goes here
            print "Someone is clicked, don't know who :("

Any suggestions?

Thank you and best regards!


Solution

  • Got some clues from my colleague. We can use the built-in change dictionary to track the event.

    The complete code list:

    from enaml.widgets.api import (
        Window, Container, PushButton)
    
    enamldef TwoButtons(Container):  
        attr cont
        PushButton:        
            text = 'Button1'
            clicked :: cont.clicked(1)
    
        PushButton:
            text = 'Button2'
            clicked :: cont.clicked(2)
    
    
    enamldef Main(Window):
        Container:
            attr buttonId
            event clicked
    
            TwoButtons:
                cont = parent
    
            clicked ::
                print change.get('value')
                print "I know it's you {i:s}".format(s=change['value'])