【发布时间】:2017-11-25 19:03:58
【问题描述】:
我已经坚持了好几个小时了。 我正在制作一个自制的智能家居终端,并且已经对 kivy 进行了大约 2 周的修补,到目前为止一切都很好。我正处于想要在屏幕内的标签内显示温度的地步。我制作了一个带有 4 个按钮的操作栏,单击时会在屏幕上滑动。在名为“Thermostat”的第一个屏幕中,我想显示一个标签,其中包含从我编写的另一个外部脚本读取的温度。我似乎无法获得标签内的温度,即使是虚拟值' 这是我的 main.py:
#!/usr/bin/env python3
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
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.lang import Builder
class Menu(BoxLayout):
manager = ObjectProperty(None)
def __init__(self,**kwargs):
super(Menu, self).__init__(**kwargs)
Clock.schedule_interval(self.getTemp, 1)
def getTemp(self,dt):
thetemp = 55 #will be changed to temp.read()
self.ids.TempLabel.text = str(thetemp)
print(thetemp)
class ScreenThermo(Screen):
pass
class ScreenLight(Screen):
pass
class ScreenEnergy(Screen):
pass
class ScreenWeather(Screen):
pass
class Manager(ScreenManager):
screen_thermo = ObjectProperty(None)
screen_light = ObjectProperty(None)
screen_energy = ObjectProperty(None)
screen_weather = ObjectProperty(None)
class MenuApp(App):
def thermostaat(self):
print("Thermostaat")
def verlichting(self):
print("Verlichting")
def energie(self):
print("Energie")
def weer(self):
print("Het Weer")
def build(self):
Builder.load_file("test.kv")
return Menu()
if __name__ == '__main__':
MenuApp().run()
这是我的 .kv 文件:
#:kivy 1.10.0
<Menu>:
manager: screen_manager
orientation: "vertical"
ActionBar:
size_hint_y: 0.05
ActionView:
ActionPrevious:
ActionButton:
text: "Thermostaat"
on_press: root.manager.current= 'thermo'
on_release: app.thermostaat()
ActionButton:
text: "Verlichting"
#I want my screens to switch when clicking on this actionbar button
on_press: root.manager.current= 'light'
on_release: app.verlichting()
ActionButton:
text: "Energieverbruik"
on_press: root.manager.current= 'energy'
on_release: app.energie()
ActionButton:
text: "Het Weer"
on_press: root.manager.current= 'weather'
on_release: app.weer()
Manager:
id: screen_manager
<ScreenThermo>:
Label:
#this is where i want my label that shows the temperature my sensor reads
text: "stuff1"
<ScreenLight>:
Button:
text: "stuff2"
<ScreenEnergy>:
Button:
text: "stuff3"
<ScreenWeather>:
Button:
text: "stuff4"
<Manager>:
id: screen_manager
screen_thermo: screen_thermo
screen_light: screen_light
screen_energy: screen_energy
screen_weather: screen_weather
ScreenThermo:
id: screen_thermo
name: 'thermo'
manager: screen_manager
ScreenLight:
id: screen_light
name: 'light'
manager: screen_manager
ScreenEnergy:
id: screen_energy
name: 'energy'
manager: screen_manager
ScreenWeather:
id: screen_weather
name: 'weather'
manager: screen_manager
我经常收到以下错误:
super(ObservableDict, self).__getattr__(attr))
AttributeError: 'super' object has no attribute '__getattr__'
如果您想知道,这是我的回溯:
Traceback (most recent call last):
File "main.py", line 57, in <module>
MenuApp().run()
File "/home/default/kivy/kivy/app.py", line 829, in run
runTouchApp()
File "/home/default/kivy/kivy/base.py", line 502, in runTouchApp
EventLoop.window.mainloop()
File "/home/default/kivy/kivy/core/window/window_pygame.py", line 403, in mainloop
self._mainloop()
File "/home/default/kivy/kivy/core/window/window_pygame.py", line 289, in _mainloop
EventLoop.idle()
File "/home/default/kivy/kivy/base.py", line 337, in idle
Clock.tick()
File "/home/default/kivy/kivy/clock.py", line 581, in tick
self._process_events()
File "kivy/_clock.pyx", line 367, in kivy._clock.CyClockBase._process_events
cpdef _process_events(self):
File "kivy/_clock.pyx", line 397, in kivy._clock.CyClockBase._process_events
raise
File "kivy/_clock.pyx", line 395, in kivy._clock.CyClockBase._process_events
event.tick(self._last_tick)
File "kivy/_clock.pyx", line 167, in kivy._clock.ClockEvent.tick
ret = callback(self._dt)
File "main.py", line 17, in getTemp
self.ids.TempLabel.text = str(thetemp)
File "kivy/properties.pyx", line 839, in kivy.properties.ObservableDict.__getattr__
super(ObservableDict, self).__getattr__(attr))
AttributeError: 'super' object has no attribute '__getattr__'
我希望有人愿意帮助我,因为我想继续这个项目,我的智能家居设备的大部分功能已经可以使用,所以最后一部分是制作一个合适的 GUI这一切都在(控制灯光,控制房子里的温度等......)
【问题讨论】:
标签: python user-interface raspberry-pi kivy kivy-language