Python 和 Phidg​​ets:想要将事件/消息从对象发送到其父级

发布于 2024-10-31 10:18:35 字数 372 浏览 7 评论 0原文

我有一个 Phidg​​ets 步进控制器(Stepper 类),它允许事件处理程序成为该类的方法:

self.setOnAttachHandler(self.StepperAttached)
self.setOnDetachHandler(self.StepperDetached)

这些方法很有用,因为它们可以在步进控制器与 PC 连接/分离时执行任务。

我已经在 Python 中的 wxFrame 中创建了一个 Stepper 对象,并且想知道如何将这些消息发送到 wxFrame,以便它可以指示控制器已连接/分离而无需轮询。

或者一般来说,如何在 Python 中将事件/消息从对象发送到父对象?

谢谢!

I have a Phidgets stepper controller (Stepper class) and it allows event handlers to be methods of the class:

self.setOnAttachHandler(self.StepperAttached)
self.setOnDetachHandler(self.StepperDetached)

These are useful as they can perform tasks when the stepper controller is attached/detached from the PC.

I have created a Stepper object in a wxFrame in Python and would like to know how to send those messages to the wxFrame so that it can, for example, indicated that the controller has been attached/dettached without polling.

Or in general, how do I send events/messages from object to parent in Python?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长梦不多时 2024-11-07 10:18:35

执行此操作的规范方法是将父对象的引用传递给子对象。

from Phidgets.Devices.Stepper import Stepper

class Parent(object):
    "Parent Class"

    def stepperAttached(self, event):
        print 'Connected to device ', event.device.getSerialNum()

    def eventHandler (self, event):
        print "Event fired!", event.state

class Child(object):
    "Child class"

    def __init__(self, parent):
        self.parent = parent
        self.stepper =  Stepper()
        self.stepper.setOnAttachHandler(self.parent.StepperAttached)
        self.stepper.setOnInputChangedHandler(self.parent.eventHandler)

p = Parent()
c = Child(p)
c2 = Child(p)
# etc..

希望这有帮助。代码可能不太准确,因为我手边没有参考资料,但即使上面的代码略有偏差,原理也是相同的。

The canonical way to do this is to pass a reference of the parent object down into the children.

from Phidgets.Devices.Stepper import Stepper

class Parent(object):
    "Parent Class"

    def stepperAttached(self, event):
        print 'Connected to device ', event.device.getSerialNum()

    def eventHandler (self, event):
        print "Event fired!", event.state

class Child(object):
    "Child class"

    def __init__(self, parent):
        self.parent = parent
        self.stepper =  Stepper()
        self.stepper.setOnAttachHandler(self.parent.StepperAttached)
        self.stepper.setOnInputChangedHandler(self.parent.eventHandler)

p = Parent()
c = Child(p)
c2 = Child(p)
# etc..

Hopefully this helps. Code might not be quite accurate, as I don't have a reference handy, but the principal is the same even if the code above is slightly off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文