stephenlendl
stephenlendl

Reputation: 33

Updating a displayed variable every second with Clock.schedule_interval

I have schedule_interval calling a function that fetches weather data from the web and then parses it into a dict. I have my kv file reading that dict and displaying values in a floatlayout. I know the function is being called because I am having it print to the console also, but it is not updating in the floatlayout window. I thought the values would automatically update from what I have read.

GUI.py
class weather(FloatLayout):
    def w(self):
        a = parse()
        print(a)
        return a

class weatherApp(App):
    def build(self):
        d = weather()
        Clock.schedule_interval(d.w, 1)
        return d

weather.kv
<Layout>:
     DragLabel:
        font_size: 600
        size_hint: 0.1, 0.1
        pos: 415,455
        text: str(root.w()['temp0'])

This is just one of the labels. I am very new to Kivy so if this looks atrocious to you experienced kivy people, I apologize.

The print(a) part of def w(self): works every second, but the window does not display the new variables.

test.py

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout

a = {}
a['num'] = 0

class test(FloatLayout):
    def w(self):
        a['num'] += 1
        print(a['num'])
        return a

class testApp(App):
    def build(self):
        d = test()
        Clock.schedule_interval(test.w, 1)
        return d

if __name__ == '__main__':
    p = testApp()
    p.run()


test.kv

#:kivy 1.10.1

<Layout>:
    Label:
        font_size: 200
        size_hint: 0.1, 0.1
        pos: 415,455
        text: str(root.w()['num'])

Upvotes: 3

Views: 528

Answers (1)

eyllanesc
eyllanesc

Reputation: 244003

It seems that you have several misconceptions:

  • If you invoke a function in python it does not imply that the .kv will be called. So if you call the w method with Clock.schedule_interval() it does not imply that the calculated value updates the value of the Label text.

  • When you call a function with Clock.schedule_interval you must use the object not a class. in your case, test is class and instead, d is the object.

When you call a function with Clock.schedule_interval you must use the object not a class. in your case, test is class and instead, d is the object.

*.py

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import DictProperty


class test(FloatLayout):
    a = DictProperty({"num": 0})

    def w(self, dt):
        self.a["num"] += 1
        print(self.a["num"])


class testApp(App):
    def build(self):
        d = test()
        Clock.schedule_interval(d.w, 1)
        return d


if __name__ == "__main__":
    p = testApp()
    p.run()

*.kv

#:kivy 1.10.1

<Layout>:
    Label:
        font_size: 200
        size_hint: 0.1, 0.1
        pos: 415,455
        text: str(root.a["num"])

Upvotes: 1

Related Questions