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

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

问题描述

我正在努力处理回调对象,我是新手,请保持友好:

I am struggling with callback objects, I am a newbie please be nice:

我正在使用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)


EDIT


EDIT

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

您当然可以将参数设为全局变量,但您只能将全局变量用作最后的选择或用于常量.

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天全站免登陆