alkopop79
alkopop79

Reputation: 539

Update value in Kivy (Python) widget

I'd like to display numeric values coming from a sensor (Tinkerforge ambient light sensor) in Kivy widgets. Unfortunately the variable 'illuminance' doesn't seem to change at all and the widgets display '0'.'illuminance' seems to update nicely and gets printed on the console. What am I doing wrong?

import kivy
import random
from kivy.clock import Clock
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout

from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_dc import BrickDC
from tinkerforge.bricklet_ambient_light import BrickletAmbientLight

HOST = "localhost"          # Tinkerforge IP and port
PORT = 4223
UID = "uM9"             # sensor ID

illuminance = 0

ipcon = IPConnection()          # Create IP connection
al = BrickletAmbientLight(UID, ipcon)   # Create device object
ipcon.connect(HOST, PORT)       # Connect to brickd

class TimerTink:
    def tinker(self):
        illuminance = al.get_illuminance()  #read sensor value
        print(illuminance)

class TinkerApp(App):
    def build(self):
        main_layout = BoxLayout(padding=10, orientation="vertical")
        for i in range(2):
            h_layout = BoxLayout(padding=10)
            for i in range(2):
                lbl = Label(text=str(illuminance),)
                h_layout.add_widget(lbl)
            main_layout.add_widget(h_layout)
        event = Clock.schedule_interval(TimerTink.tinker, 1/2)
        return main_layout

if __name__ == "__main__":
    app = TinkerApp()
    app.run()

Finally it's working, thanks to @YOSHI 's suggestions:

import kivy
import random
from kivy.clock import Clock
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout

from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_dc import BrickDC
from tinkerforge.bricklet_ambient_light import BrickletAmbientLight

HOST = "localhost"          # Tinkerforge IP and port
PORT = 4223
UID = "uM9"             # sensor ID

illuminance = 0

ipcon = IPConnection()          # Create IP connection
al = BrickletAmbientLight(UID, ipcon)   # Create device object
ipcon.connect(HOST, PORT)       # Connect to brickd


class TinkerApp(App):

    def build(self):
        i = 0
        main_layout = BoxLayout(padding=10, orientation="vertical")
        h_layout = FloatLayout(size=(300,300))
        self.label = Label(text=str(illuminance),pos=(i*100, i*100),size_hint = (.1,.1))
        h_layout.add_widget(self.label)
        main_layout.add_widget(h_layout)
        Clock.schedule_interval(self.timer, 0.1)

        return main_layout

    def timer(self, dt):
        illuminance = al.get_illuminance()  #read sensor value
        self.label.text = str(illuminance)
        print(str(illuminance))

if __name__ == "__main__":
    app = TinkerApp()
    app.run()

Upvotes: 1

Views: 1094

Answers (1)

joscha
joscha

Reputation: 1183

The function 'tinker' in the TimeTink class takes 2 arguments: self and dt

def tinker(self,dt):
    illuminance = al.get_illuminance()  #read sensor value
    print(illuminance)

or you change the line where you call the method to sth. like this:

event = Clock.schedule_interval(lambda dt: TimerTink.tinker, 1/2)

for more info visit the doc for kivy.clock.

Edit: (maybe this works)

import kivy
import random
from kivy.clock import Clock
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout


from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_dc import BrickDC
from tinkerforge.bricklet_ambient_light import BrickletAmbientLight

HOST = "localhost"          # Tinkerforge IP and port
PORT = 4223
UID = "uM9"             # sensor ID

illuminance = 0

ipcon = IPConnection()          # Create IP connection
al = BrickletAmbientLight(UID, ipcon)   # Create device object
ipcon.connect(HOST, PORT)       # Connect to brickd


main_layout = BoxLayout(padding=10, orientation="vertical")
for i in range(2):
    h_layout = BoxLayout(padding=10)
    for i in range(2):
        lbl = Label(text=str(illuminance),)
        h_layout.add_widget(lbl)
    main_layout.add_widget(h_layout)
def tinker(self):
    illuminance = al.get_illuminance()  #read sensor value
    lbl.text = str(illuminance)
    print(illuminance)

class TinkerApp(App):
    def build(self):
        event = Clock.schedule_interval(tinker, 1/2)
        return main_layout

if __name__ == "__main__":
    app = TinkerApp()
    app.run()

Upvotes: 1

Related Questions