gpt4 book ai didi

python - 更新 Kivy (Python) 小部件中的值

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:19 27 4
gpt4 key购买 nike

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

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

最佳答案

TimeTink 类中的函数“tinker”有 2 个参数: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()

关于python - 更新 Kivy (Python) 小部件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47269605/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com