0

The code below is part of my sensor GUI display program, and it is "working", but I cannot get the values I really want to work in the graphs. I get a NameError: name 'result' is not defined.

I want the value result to display in the field rather than accel0.getAcceleration() I can get it to print no problem, and tried global values, changing number format, etc to the best of my abilities, but just can't get it to work. Any help would be greatly appreciated!!!

def onAccelerationChange(self, acceleration, timestamp):
    global result
    xA = float(acceleration[0])
    xZ = float(acceleration[2])
    xAngle = xA / xZ
    rads = (math.atan(xAngle))
    result = math.degrees(rads)  ***I want this value to go...***


def plot(self):
    x = ["Load 0", "Load 1", "Load 2", "Load 3"]
    h = [(load0.getVoltageRatio() * 1000), (load1.getVoltageRatio() * 1000), (load2.getVoltageRatio() * 1000), (load3.getVoltageRatio() * 1000)]

    # adding the subplot
    self.plot1.cla()
    self.plot1.bar(x, h)
    self.plot1.set_xlabel("Strain Gauges")
    self.plot1.set_ylabel("PLI")
    self.plot1.set_title("Blade Load")

    Angle = "Angle: " + str***(accel0.getAcceleration())*** + "°"  
    self.text['text'] = Angle

    self.canvas.draw()
    self.canvas.get_tk_widget().pack()
Marcus
  • 2,153
  • 2
  • 13
  • 21

1 Answers1

0

You can define global outside of the function initially. You can read about global here:

Using global variables in a function

You should not use a global for this, though, you should use a class instance variable, since you already have a class. See this question for more info: Instance variables vs. class variables in Python

This all assumes the code you posted is part of a class, and that you have a main function somewhere calling the first function before the second.

ᴓᴓᴓ
  • 1,178
  • 1
  • 7
  • 18