Kivy - 用传感器数据更新标签?

时间:2017-11-24 08:46:32

标签: python label kivy

kivy和OOP的新手。

我正在尝试使用从温度传感器拉出的数据来更新kivy中的标签。拉入传感器数据的代码在labeltempmod中。我创建了一个每秒调用的函数getTheTemp()。在函数中,我尝试通过Label(text =(format(thetemp)),font_size = 80)分配标签的文本。该程序忽略了这一点。我在这里做错了什么?

#This is a test to see if I can write the temp to label
import labeltempmod
import kivy

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

def getTheTemp(dt):
        thetemp =  labeltempmod.readtemp()
        Label(text=(format(thetemp)), font_size=80)
        print thetemp

class LabelWidget(BoxLayout):
    pass

class labeltestApp(App):
    def build(self):

        # call get_temp 0.5 seconds
        Clock.schedule_interval(getTheTemp, 1)

        return LabelWidget()

if __name__ == "__main__":
    labeltestApp().run()

这是kivy语言文件:

<LabelWidget>:
        orientation: 'vertical'
        TextInput:
                id: my_textinput
                font_size: 80
                size_hint_y: None
                height: 100
                text: 'default'
        FloatLayout:
                Label:
                        id: TempLabel
                        font_size: 150
                        text: 'Temp Test'

感谢。

1 个答案:

答案 0 :(得分:1)

抱歉,您永远不会更新某些内容您只是在创建另一个标签

试试这个:

2017-11-24
2017-11-24 00:00:00
2017-12-22 00:00:00
2017-08-31
[u'31.08.17']
Traceback (most recent call last):
 File "comment.py", line 52, in <module>
 date2 = datetime.datetime.strptime(str(date), '%Y-%m-%d')
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
 (data_string, format))
ValueError: time data [selenium.webdriver.remote.webelement.WebElement (session="5395379b492ea99d3df3ffaf80718908", element="0.7086675347389584-2")] does not match format '%Y-%m-%d'

更新:对于您的上一次请求,我认为最好的方法是:

class LabelWidget(BoxLayout):

    def __init__(self, **kwargs):
        super(LabelWidget, self).__init__(**kwargs)
        Clock.schedule_interval(self.getTheTemp, 1)

    def getTheTemp(self, dt):
        thetemp =  labeltempmod.readtemp()
        self.ids.TempLabel.text = thetemp
        print thetemp

class labeltestApp(App):

    def build(self):
        return LabelWidget()

if __name__ == "__main__":
    labeltestApp().run()