我是编程新手,并且一直尝试用Raspberry Pi制作这个项目超过一年。问题是我似乎无法让我的标签自动更改。我尝试过Clock.schedule_interval,但它什么也没做。也许我一直在错误的地方尝试?
main.py
#!/usr/bin/env python
import os
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.clock import Clock
def currtempa():
tfile = open(r"/home/pi/loftpi/w1_slave")
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000
temperature = round(temperature, 1)
temperature = float(temperature)
return temperature
Clock.schedule_interval(lambda dt: currtempa(), 0.5)
def outtempa():
tfile = open(r"/sys/bus/w1/devices/28-0315907fa7ff/w1_slave")
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000
temperature = round(temperature, 1)
temperature = float(temperature)
return temperature
class MainWidget(Widget):
def tempten(args):
f = open(r"/var/bin/thermostat", "r+")
value = float(f.read())
f.seek(0)
f.write(str(10.0))
def tempfifteen(args):
f = open(r"/var/bin/thermostat", "r+")
value = float(f.read())
f.seek(0)
f.write(str(15.0))
def tempseventeen(args):
f = open(r"/var/bin/thermostat", "r+")
value = float(f.read())
f.seek(0)
f.write(str(17.0))
def tempeightteen(args):
f = open(r"/var/bin/thermostat", "r+")
value = float(f.read())
f.seek(0)
f.write(str(18.0))
def tempnineteen(args):
f = open(r"/var/bin/thermostat", "r+")
value = float(f.read())
f.seek(0)
f.write(str(19.0))
def temptwenty(args):
f = open(r"/var/bin/thermostat", "r+")
value = float(f.read())
f.seek(0)
f.write(str(20.0))
def temptwentyone(args):
f = open(r"/var/bin/thermostat", "r+")
value = float(f.read())
f.seek(0)
f.write(str(21.0))
def temptwentytwo(args):
f = open(r"/var/bin/thermostat", "r+")
value = float(f.read())
f.seek(0)
f.write(str(22.0))
def settemp(args):
f = open(r"/var/bin/thermostat", "r+")
value = float(f.read())
return value
outtemp = outtempa()
currtemp = currtempa()
class myApp(App):
def build(self):
return MainWidget()
if __name__ == "__main__":
myApp().run()
我的kv
#:kivy 1.9.2
<MainWidget>:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'images/home.png'
GridLayout:
size: root.size
rows:3
BoxLayout:
size: self.size
Label:
id: current
text: str(root.currtemp)
Label:
id: status
text: ""
Image:
source: 'images/flame.png'
center:self.parent.center
allow_stretch: True
Label:
id: outside
text: str(root.outtemp)
BoxLayout:
size: self.size
Label:
id: sett
text: str(root.settemp())
BoxLayout:
size: self.size
spacing: 30
padding: 30
size_hint_y: .70
Button:
on_press: root.tempten()
id: ten
Image:
source: 'images/10.png'
y: self.parent.y
x: self.parent.x
allow_stretch: True
size: 65, 65
Button:
on_press: root.tempfifteen()
id: fifteen
Image:
source: 'images/15.png'
y: self.parent.y
x: self.parent.x
allow_stretch: True
size: 65, 65
Button:
on_press: root.tempseventeen()
id: seventeen
Image:
source: 'images/17.png'
y: self.parent.y
x: self.parent.x
allow_stretch: True
size: 65, 65
Button:
on_press: root.tempeightteen()
id: eighteen
Image:
source: 'images/18.png'
y: self.parent.y
x: self.parent.x
allow_stretch: True
size: 65, 65
Button:
on_press: root.tempnineteen()
id: nineteen
Image:
source: 'images/19.png'
y: self.parent.y
x: self.parent.x
allow_stretch: True
size: 65, 65
Button:
on_press: root.temptwenty()
id: twenty
Image:
source: 'images/20.png'
y: self.parent.y
x: self.parent.x
allow_stretch: True
size: 65, 65
Button:
on_press: root.temptwentyone()
id: twentyone
Image:
source: 'images/21.png'
y: self.parent.y
x: self.parent.x
allow_stretch: True
size: 65, 65
Button:
on_press: root.temptwentytwo()
id: twentytwo
Image:
source: 'images/22.png'
y: self.parent.y
x: self.parent.x
allow_stretch: True
size: 65, 65
答案 0 :(得分:0)
你需要将outtemp和currtemp作为StringProperty()
像这样:
from kivy.properties import StringProperty
class MainWidget(Widget):
outtemp = StringProperty()
currtemp = StringProperty()
确保函数返回字符串
return str(temperature)
那就是说,我会写这个有点不同。
在这种情况下,我会将函数作为类方法而不是具有全局函数。 然后在课堂内启动时钟。这样你可以做这样的事情。
def __init__(self,**kwargs):
super(MainWidget,self).__init__(**kwargs)
Clock.schedule_interval(self.currtempa, 0.5)
def currtempa(self,*args):
"""code"""
self.currtemp = str(ctemp)