我遇到了一个关于kivy脚本的AssertionError。代码如下:
import time
from copy import deepcopy as dc
# import modules related to kivy UI
from kivy.app import App
# kivy.require("1.9.1")
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.image import Image
# define global variables just for testing
temperature = str(25)+"°C"
humidity = str(95)+'%'
last_water = "15/04/2018 15:40"
time_next = str(4)+' hr'
kv = """
<LeftScreen>:
GridLayout:
cols: 2
Image:
id: im
source: 'C:/Users/Acer/Documents/Python Scripts/sakura.jpg'
size_hint: 0.5, 0.5
Label:
id: title
text: '[b][i]Smart[/i]'+' Gardener[/b]'
color: (0.25,0.5,0.1,1)
font_size: 60
font_name: 'times'
Label:
id: group
text: '[i]group 8[/i]'
color: (0.3,0.3,0.6,1)
font_size: 45
font_name: 'Arial'
valign: 'bottom'
Label:
id: lastU
text: '[i]'+'Last Update: '+time.striftime("%H:%M:%S")+'[/i]'
font_size: 18
halign: 'center'
valign: 'middle'
Button:
id: update
text: 'Refresh'
font_size: 20
halign: 'center'
valign: 'middle'
on_press: root.reload()
Label:
id: temp_lbl
text: 'Temperature: '
font_size: 24
halign: 'center'
valign: 'middle'
Label:
id: temp_val
text: root.temp
font_size: 24
halign: 'center'
valign: 'middle'
Label:
id: hum_lbl
text: 'Humidity: '
font_size: 24
halign: 'center'
valign: 'middle'
Label:
id: hum_val
text: root.hum
font_size: 24
halign: 'center'
valign: 'middle'
Label:
id: lastW_lbl
text: 'Last Watering: '
font_size: 24
halign: 'center'
valign: 'middle'
Label:
id: lastW_val
text: root.last_water
font_size: 24
halign: 'center'
valign: 'middle'
Label:
id: nextW_lbl
text: 'Time before Next Watering: '
font_size: 24
halign: 'center'
valign: 'middle'
Label:
id: nextW_val
text: root.time_text
font_size: 24
halign: 'center'
valign: 'middle'
"""
Builder.load_string(kv)
# create the left screen
class LeftScreen(Screen):
def __init__(self, **kwargs):
super(LeftScreen, self).__init__(**kwargs)
self.temp = dc(temperature)
self.hum = dc(humidity)
self.last_water = dc(last_water)
self.time_next = dc(time_next)
def reload(self,value):
self.ids.lastU.text='Last Update: '+time.strftime("%H:%M:%S")
self.ids.temp_val.text=temperature
self.ids.hum_val.text=humidity
self.ids.lastW_val.text=last_water
self.ids.nextW_val.text=time_next
class RightScreen(Screen):
def __init__(self,**kwargs):
super(RightScreen,self).__init__(**kwargs)
self.layout = GridLayout()
self.add_widget(self.layout)
class SmartGardener(App):
def build(self):
sm = ScreenManager()
ls = LeftScreen(name='data')
sm.add_widget(ls)
sm.current = 'data'
return sm
def reset():
import kivy.core.window as window
from kivy.base import EventLoop
if not EventLoop.event_listeners:
from kivy.cache import Cache
window.Window = window.core_select_lib('window', \
window.window_impl, True)
Cache.print_usage()
for cat in Cache._categories:
Cache._objects[cat] = {}
myApp = SmartGardener()
if __name__ == '__main__':
reset()
myApp.run()
我插入的图像或我用于标签的变量是否有错误?我想插入图像作为UI的背景。
此外,变量&#39; time_next&#39;似乎有些错误。当我试图从kivy文件访问它时。访问属性的正确方法是什么?time_next&#39; kv引用中的对象MainScreen?
答案 0 :(得分:0)
好的,我试图运行你的代码,这就是我发现的:
您尝试访问&#39; time_text&#39;在kv文件中,当你在课程中设置时间&#39;,这是一个错字,但即使在我修复它之后我还必须在你调用的所有变量的kv文件中设置默认值,哼, temp,time_next,last_water:
<LeftScreen>:
time_text: ''
last_water:''
hum: ''
temp: ''
GridLayout:
cols: 2
...
接下来你使用time.striftime这是一个错误的time.strftime,但你需要在kv代码中导入它,如下所示:
:import strftime time.strftime
<LeftScreen>:
...
并使用它:
Label:
id: lastU
text: '[i]'+'Last Update: '+strftime("%H:%M:%S")+'[/i]'
请参阅kv语言文档:special syntax
我还没有能够运行你的代码,我没有你使用的字体,所以我只是评论了这些线条,一旦它运行我发现标签&#39; s显示标记,要使用它,您需要明确设置markupe=True
Label:
id: title
text: '[b][i]Smart[/i]'+' Gardener[/b]'
markup: True
color: (0.25,0.5,0.1,1)
font_size: 60
#font_name: 'times'
Label:
id: group
text: '[i]group 8[/i]'
markup: True
color: (0.3,0.3,0.6,1)
font_size: 45
#font_name: 'Arial'
valign: 'bottom'
最后,单击刷新按钮会调用reload而没有任何值,而它需要一些值,因此刷新&#39;按钮会引发TypeError。