更新Kivy(Python)小部件

时间:2017-11-13 16:56:08

标签: python kivy

我想在Kivy小部件中显示来自传感器(Tinkerforge环境光传感器)的数值。不幸的是,变量'illuminance'似乎根本没有变化,小部件显示为'0'。'illuminance'似乎很好地更新并在控制台上打印。我究竟做错了什么?

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()

最后它正在运作,感谢@YOSHI的建议:

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()

1 个答案:

答案 0 :(得分:1)

功能' tinker'在TimeTink类中有两个参数:self和dt

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

或者您将调用方法的行更改为某位。像这样:

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

有关详细信息,请访问kivy.clock的文档。

编辑:(也许这有效)

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()