Events with Global Variables or Class Methods

Supporting 2.7 and 3.2+
Post Reply
dpastl
Fresh meat
Posts: 2
Joined: Wed Oct 31, 2018 9:17 am
Contact:

Events with Global Variables or Class Methods

Post 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!
jdecoux
Labview Developer
Posts: 161
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: Events with Global Variables or Class Methods

Post 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)
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests