0

我使用python和twisted编写了一个snmp管理程序,其中一部分需要客户端上的一些按钮绑定到远程snmp set方法,这是由透视代理处理的。

for item in devicevars[current+" buttons"]:
    ttk.Label(buttonframe, text=item + ":").grid(column=1, row=i2, sticky=(E))
    ttk.Button(buttonframe,width = 3,textvariable=guivars["%s %s" %(current, 
                                item)],command=remoteButton(current, item)).grid(column=2, row=i2, sticky=(W))
    i2 = i2+1


def remoteButton(dname, value):

    rbutton= pbfactory.getRootObject()
    rbutton.addCallback(lambda object: object.callRemote("SNMP", dname, value))
    rbutton.addErrback(lambda reason: 'error: '+str(reason.value))

问题是此代码会导致远程方法在客户端启动后立即触发。有谁知道为什么会这样?

4

1 回答 1

0

(因此也是)中的command选项是一个可调用对象,当单击按钮时将调用该对象。Tkinterttk

通过传递,command=remoteButton(...)remoteButton立即调用,然后将其结果作为command选项传递。

相反,您想要做的是传递一个command选项,该选项在运行时将调用remoteButton.

这样做的方法是:

ttk.Button(buttonframe,width = 3,
           textvariable=guivars["%s %s" %(current, item)],
           command=lambda: remoteButton(current, item))

换句话说,lambda:在它前面加上a,你会得到一个调用它的函数,而不是仅仅调用它。

于 2012-10-24T08:15:00.220 回答