Skip to main content
 首页 » 编程设计

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

2024年10月25日8mengfanrong

我正在使用 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 :(" 

有什么建议吗?

谢谢你和最好的问候!

请您参考如下方法:

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

完整代码 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'])