【问题标题】:Kivy - Update a label with sensor data?Kivy - 使用传感器数据更新标签?
【发布时间】:2017-11-24 08:46:32
【问题描述】:

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'

谢谢。

【问题讨论】:

    标签: python label kivy


    【解决方案1】:

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

    试试这个:

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

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

    ...
    class LabelWidget(BoxLayout):
    
        def __init__(self, **kwargs):
            super(LabelWidget, self).__init__(**kwargs)
            self.Thetemp = None
            Clock.schedule_interval(self.getTheTemp, 1)
    
        def getTheTemp(self, dt):
            if self.Thetemp is None:
                self.thetemp =  labeltempmod.readtemp()
            else:
                self.thetemp =  labeltempmod.readtemp(self.theTemp)
            self.ids.TempLabel.text = str(self.thetemp)
    

    【讨论】:

    • 谢谢你,你这个好人!这样可行。我不得不向感兴趣的人添加格式(thetemp)。标签文本只接受字符串。 OOP 让这个菜鸟有点困惑。但我会继续努力弄清楚。
    • 我使用的温度传感器不是很精确。我想介绍一个一阶滞后滤波器来平滑读数。这引入了将旧的 temp 传递回 labeltempmod.readtemp() 的需要。这意味着 getTheTemp(self, dt) 需要返回温度。 getTheTemp(self, dt) 由 clock.schedule 行调用。如何将 temp 返回到 init 函数,以便在下次调用它时将其发送回 getTheTemp,以便将其发送到 labeltempmod.readtemp(thetemp)?
    • 感谢您抽出宝贵时间帮助我。那行得通!我现在可以传递数据了。因此,如果我正确理解这一点,向后工作,就会运行 labeltestApp()。 labeltestApp() 有一个名为 build() 的方法,它返回 LabelWidget()。当 labeltestApp() 返回 LabelWidget() 时它创建了 LabelWidget() 的实例?而且这个实例被保存在内存中永远不会被破坏,所以实例中的 self.theTemp 变量总是在内存中并且可以随时调用?为什么甚至需要 labeltestApp()?为什么不直接调用 LabelWidget().run()?
    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2017-03-13
    • 2020-04-08
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多