Page 1 of 1

Events with Global Variables or Class Methods

Posted: Wed Oct 31, 2018 11:47 am
by dpastl
Hello,

I'm trying to write a simple application and I'm running into a couple of problems that I thought someone might be able to shine light one.

First is that I can't seem to access global variables through the event handlers. For example:

Code: Select all

cartridgecount = 0

def ontrigger(self, state):
    cartridgecount += 1
Will not work. Is there a way to do this or achieve similar functionality where a handler is able to modify something that the main process is accessing?

The second problem is somewhat related, but I can't figure out how to add a class method as an event handler, which could potentially solve this issue, although I'm assuming some mutex locks would be needed still.

Thanks!

Re: Events with Global Variables or Class Methods

Posted: Thu Nov 01, 2018 9:36 am
by jdecoux
In Python, you need to declare a variable as global to use the global version of it.

Code: Select all

cartridgecount = 0

def ontrigger(self, state):
    global cartridgecount 
    cartridgecount += 1
It is possible to use a class method as an event handler. In Python, you will need to prepend the class's self parameter to the event handler's arguments. For example:

Code: Select all

class myClass:
    def __init__(self, a):
        self.a = a
    
    def onVoltageChangeHandler(self, voltageRatioObject, voltageRatio):
        print(self.a)
        print(voltageRatioObject.getDeviceSerialNumber())
        print(voltageRatio)

vi = VoltageInput()
classA = myClass("A")
vi.setOnVoltageChangeHandler(classA.onVoltageChangeHandler)