简体   繁体   中英

AssertionError in Python / Kivy

I came across an AssertionError for a kivy script. The codes as follows:

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

Is the error with the image that I inserted or the variables I used for the labels? I want to insert the image as the background of the UI.

Also, there seems to be some error with the variable 'time_next' when I tried to accessing it from the kivy file. What is the right way to access the attribute 'time_next' of object MainScreen in the kv quote?

Okay, i tried to run your code and here is what i found:

  • you try to access 'time_text' in kv file, while you set in the class 'time_next', that's a typo but even after i fixed it i had to set defaults in the kv file for all the variables that you call, hum, temp, time_next, last_water:

     <LeftScreen>: time_text: '' last_water:'' hum: '' temp: '' GridLayout: cols: 2 ... 
  • next you use the time.striftime which is a typo to time.strftime, yet you need to import it in the kv code like so:

     :import strftime time.strftime <LeftScreen>: ... 

    and use it like:

     Label: id: lastU text: '[i]'+'Last Update: '+strftime("%H:%M:%S")+'[/i]' 

    see the kv language docs for it: under special syntax

  • i wasn't able to run your code yet, i didn't have the Fonts you used, so i simply commented those lines, and once it ran i noticed that the Label's show the markup, to use it you need to explicitly set 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' 

At last, clicking the refresh button calls reload without any value, while it expects some value, so the 'refresh' button would raise a TypeError.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM