Skip to content
Advertisement

Cant wrap my head around Classes and Kivy

I am trying to use kivy.clock object more specifically Clock.schedule_interval to take temperature readings every couple seconds.

I create a method in my main screen class (MyTerrLayout(Screen)) called clockAction. now when I type in Clock.schedule_interval(clockAction, 2) I get:

NameError: clockAction is not defined

So I tired to do self.clockAction but that didn’t work either. I tried various methods to get it going like moving Clock.schedule_interval(clockAction, 2) to its own class but I get other errors. Do i have to create an instance of the method clockAction? Or since Clock.schedule_interval(clockAction, 2) might be a class attribute it needs to be called a different way.

Or is it because Clock.schedule_interval(clockAction, 2) is a class attribute that I need to call clockAction a different way? I can’t seem to make the connection.

If someone could point me in the right direction that would be awesome. Also does anyone know where I can practice more complex examples of OOP and class manipulation? Or does someone have some old homework regarding class manipulation and OOP concepts?

Here is my code

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from random import randint
from kivy.lang import Builder  
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
import threading

Window.size = (600,250)

tep = 75
hum = 85
# Window Manager Class
class WindowManager(ScreenManager):
    pass    


# Main home menu Class
class MyTerrLayout(Screen):
    internal_read = ObjectProperty(None)
    external_read = ObjectProperty(None)
    target_temp = ObjectProperty(None)
    target_hum = ObjectProperty(None)

    Clock.schedule_interval(clockAction, 2) #Clock Schedule

    def set_temp(self):
        global tep
        self.ids._Target_Temp.text = str(tep) + " F" 

    def set_hum(self):
        global hum
        self.ids._Target_Hum.text = str(hum) + "%"
    
    def clockAction(self, dt=0): ####method I am trying to run
        self.ids.external_reading.text = str(randint(1,10))
        print('test')


# Temprature Screen Class / Window  
class TempWindow(Screen):
    def sub_temp(self):
        global tep
        tep = tep - 1
        self.temp_menu.text = str(tep)

    def add_temp(self):
        global tep
        tep = tep + 1
        self.temp_menu.text = str(tep)
    def set_temp(self):
        terrlayout = self.manager.get_screen('main') 
        terrlayout.set_temp()

class HumWindow(Screen):   
    def sub_hum(self):
        global hum
        hum = hum - 1
        self.ids.set_hum_menu.text = str(hum)

    def add_hum(self):
        global hum
        hum = hum + 1
        self.ids.set_hum_menu.text = str(hum)

    def set_temp(self):
        terrlayout = self.manager.get_screen('main') 
        terrlayout.set_hum()

class AC(Screen):

    def build(self):
        Clock.schedule_interval(self.Callback_Clock, 2)

    def Callback_Clock(self, dt):
        terrlayout = self.manager.get_screen('main')
        terrlayout.clockAction()
# Builder Section
kv = Builder.load_file('terrlayoutV1.kv')

class TerrLayout(App):
    def build(self):
        
        return kv


if __name__=="__main__":
    TerrLayout().run()



Advertisement

Answer

The problem is here:

class MyTerrLayout(Screen):
    [...]
    Clock.schedule_interval(clockAction, 2)
    [...]

That code is in the class part of MyTerrLayout. In other languages, we call that static. clockAction has self as a parameter, which means it is not static.

In a static context, you can’t access members of an object, because there is no object to refer to.

IMHO this should be done when the object is created:

def __init__(self, **kw):
    super().__init__(**kw)
    Clock.schedule_interval(self.clockAction, 2)  # Clock Schedule

Note that the __init__ method has self and you can refer to self.clockAction.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement