将其他参数传递给python回调对象(win32com.client.dispatchWithEvents)

编程入门 行业动态 更新时间:2024-10-28 06:36:39
本文介绍了将其他参数传递给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 self1 = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents) self2 = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents) # Create multiple subscriptions (Note these are asynchronous) # Pushing the subscribed info is not a problem and done elsewhere self1.Subscribe(<subscription info>) self2.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)

本文发布于:2023-11-23 10:42:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   对象   参数   python   dispatchWithEvents

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!