将附加参数传递给 python 回调对象 (win32com.client.dispatchWithEvents) [英] Passing additional arguments to python callback object (win32com.client.dispatchWithEvents)

查看:34
本文介绍了将附加参数传递给 python 回调对象 (win32com.client.dispatchWithEvents)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 win32com 包与 Windows 应用程序交互(应用程序并不重要).

I am using the win32com package to interact with a windows application (The application is not important).

简而言之,我想要实现的是订阅更新的表.

In short what I am trying to achieve is a subscription to a table that updates.

我已经成功地实现了一个回调,该回调接收表更新时返回的数据,但我现在需要的是对收到的数据采取行动.

I have successfully implemented a callback that receives the returned data on an update to the table but what I need now is to act on the data received.

如果我可以用额外的参数实例化回调对象(见下面的代码),这个问题将很容易解决,但我不知道如何做到这一点.

This problem would be very easy to solve if I could instantiate the callback object with additional arguments (see code below) But I am at a loss as to how to do this.

class callBackEvents(object):
    """ Callback Object for win32com
    """

    def OnNewData(self, XMLData):
        logging.info("Subscription returned information")
        print "HERE : {}".format(XMLData))
        
        # Would like to use some argument to access logic
        # For how to use the new data  

    def OnActionResult(self, job, msg):
        return True

    def OnServerDisconnect(self):
        logging.debug("Server Disconnected")

    def OnServerConnect(self):
        logging.debug("Trader Connected To Server")


实例化回调对象:

# Instantiate API com object
self.app = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents)
# I would like to give the callback object extra arguments e.g. callBackEvents(params)


编辑实例化回调对象:

# Instatiate two com objects
self.com1 = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents)
self.com2 = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents)

# Create multiple subscriptions (Note these are asynchronous)
# Pushing the subscribed info is not a problem and done elsewhere
self.com1.Subscribe(<subscription info>)
self.com2.Subscribe(<subscription info>)

现在,当订阅信息命中回调对象时,我不知道哪个 com 对象设置了订阅(我可以根据返回的信息猜测,但这会在设置相同订阅时导致问题)

Now when subscription info hits the callback object I have no idea which com object set up the subscription (I could guess based on the information returned but this is going to cause problems when identical subscriptions are setup)

推荐答案

由于您可能只有一个应用程序实例,因此只有一个 DispatchWithEvents,您可以简单地将 params 设为类的成员:

Since you likely have only one app instance and therefore one DispatchWithEvents, you could simply make the params a member of the class:

class callBackEvents(object):
    """ Callback Object for win32com
    """

    params = None

    def OnNewData(...

    ...

# populate the params field
callBackEvents.params = yourParams

self.app = win32com.client.DispatchWithEvents("WindowsApplication", callBackEvents)

您当然可以将 params 设为全局变量,但您应该仅将全局变量用作最后的手段或作为常量使用.

You could of course make params a global but you should use globals only as a last resort or for constants.

这篇关于将附加参数传递给 python 回调对象 (win32com.client.dispatchWithEvents)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆