I'm trying to display the data from temperature sensor in my Kivy App. I found some help material from this website but I've been trying to integrate the code with my sensor but it just returns a blank screen. What am I doing wrong?
.py file:
from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.uix.label import Label
from kivy.uix.widget import Widget
import time
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()
class LabelWidget(BoxLayout):
manager = ObjectProperty(None)
def __init__(self, **kwargs):
super(LabelWidget, self).__init__(**kwargs)
Clock.schedule_interval(self.getTemp, 2)
def getTemp(self, dt):
temperature = sensor.get_temperature()
thetemp = temperature
self.manager.ids.TempLabel.text = str(thetemp)
class labeltestApp(App):
def build(self):
return LabelWidget()
if __name__ == "__main__":
labeltestApp().run()
.kv file:
LabelWidget:
Label:
id: TempLabel
text: 'temperature'
text_size: self.size
halign: 'right'
valign: 'middle'
When your application runs, there were two root instantiated. One of the root ( LabelWidget:
) is from your kv file, labeltest.kv
, and the other root is from return LabelWidget()
. The root used by your app is from return LabelWidget()
which does not has an associated modal view.
In your kv file, labeltest.kv
; replace root rule , LabelWidget:
with class rule , <LabelWidget>:
In the minimal code provided, there is no use of Kivy ScreenManager. The code, self.manager.ids.TempLabel.text = str(thetemp)
will cause an error.
Remove manager
from self.manager.ids.TempLabel.text = str(thetemp)
. The end result is self.ids.TempLabel.text = str(thetemp)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.