'NameError: name 'inslider' is not defined

Now I got a better looking new Overview of the Code:

class innentemp:
import ipywidgets as widgets from ipywidgets import interact

import matplotlib.pyplot as plt
import numpy as np

it = widgets.IntSlider(min=20, max=30, value=1, description='Innentemp:')
nt = widgets.IntSlider(min=10, max=25, value=1, description='Nachttemp:')
applyButton = widgets.Button(
    description='Update',
    disabled=False,
    button_style='', 
    tooltip='Suchen',
    icon='check', 
)
display(it, nt, applyButton)

def inside_temperature(self,it, nt):
    temp_list = []
    for i in range(8759):
        avg_temp = 0.5*(it-nt)*math.sin(((2*math.pi)/24)*(i-6))+((it-nt)*0.5)+nt
        temp_list.append(avg_temp)
    return temp_list

def apply(self):

    num_temp_list = innentemp.inside_temperature(self.it.value, self.nt.value)
    plt.plot(np.arange(8759), num_temp_list, linewidth=0.1)
    plt.show()

applyButton.on_click(apply)


 And here is the Error:
   AttributeError                            Traceback (most recent 
   call last)
   <ipython-input-10-e523fe3d0dba> in apply(self)
   26     def apply(self):
   27 
   ---> 28         num_temp_list = 
   innentemp.inside_temperature(self.it.value, self.nt.value)
   29         plt.plot(np.arange(Integer(8759)), num_temp_list, 
   linewidth=RealNumber('0.1'))
   30         plt.show()

   AttributeError: 'Button' object has no attribute 'it'

I tried putting a "self." infront of both sliders in the beginning (it, nt) but it doesn't work as well.



Solution 1:[1]

It is probably because in the function apply, you do

num_temp_list = innentemp.inside_temperature(self.it.value, self.nt.value)

You are trying to call inside_temperature from the class innentemp, but you really need to call it from self.

You need to do

num_temp_list = self.inside_temperature(self.it.value, self.nt.value)

or

num_temp_list = innentemp.inside_temperature(self, self.it.value, self.nt.value)

For example, if you have a class Person and you try

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printName(self):
        print("Name:", self.name)

    def printAge(self):
        print("Age:", self.age)

    def printData(self):
        Person.printName() # Error: missing 1 required positional argument: 'self'
        Person.printAge() # Error: missing 1 required positional argument: 'self'

john = Person("John", 20)
john.printData()

It doesn't work, because you need to call from self, not Person.

So that means this works:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printName(self):
        print("Name:", self.name)

    def printAge(self):
        print("Age:", self.age)

    def printData(self):
        self.printName()
        self.printAge()

john = Person("John", 20)
john.printData()
Name: John
Age: 20

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1