Python 将twisted matrix远程调用绑定到ttk按钮会导致远程调用在客户端启动时运行

Python 将twisted matrix远程调用绑定到ttk按钮会导致远程调用在客户端启动时运行,python,button,twisted,snmp,ttk,Python,Button,Twisted,Snmp,Ttk,Im使用python和twisted编写了一个snmp管理程序,其中一部分需要客户端上的一些按钮绑定到远程snmp集方法,这是通过透视代理处理的 for item in devicevars[current+" buttons"]: ttk.Label(buttonframe, text=item + ":").grid(column=1, row=i2, sticky=(E)) ttk.Button(buttonframe,width = 3,textvariable=guiv

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

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

问题是,这段代码会导致远程方法在客户端启动时立即启动。有人知道为什么会发生这种情况吗?

单击按钮时,将调用
Tkinter
中的
命令
选项(因此也是
ttk

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

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

方法是:

ttk.Button(buttonframe,width = 3,
           textvariable=guivars["%s %s" %(current, item)],
           command=lambda: remoteButton(current, item))
换句话说,在它前面粘贴一个
lambda:
,您将得到一个调用它的函数,而不仅仅是调用它