Enaml获取在事件处理程序中传递的参数

时间:2015-04-28 09:27:18

标签: python enaml

我正在使用Python中的Enaml设计UI。我有一个自定义控件,包括两个按钮。每次单击两个按钮中的任何一个时,一个是1,另一个是id-ed 2,我希望父容器具有单击哪个按钮的感觉。所以来自父级的事件处理程序接受一个额外的参数来区分事件的来源。这是我的代码

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 :("

有什么建议吗?

谢谢你,最诚挚的问候!

1 个答案:

答案 0 :(得分:1)

从我的同事那里得到一些线索。我们可以使用内置的change字典来跟踪事件。

完整的代码清单:

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'])