[英]Python, Kivy, "AssertionError: None is not callable" Error on function call by button
[英]AssertionError in Python / Kivy
我遇到了一個針對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的背景。
另外,當我嘗試從kivy文件訪問變量“ time_next”時,似乎出現了一些錯誤。 在kv引用中訪問對象MainScreen的屬性“ time_next”的正確方法是什么?
好的,我嘗試運行您的代碼,這是我發現的內容:
您嘗試在kv文件中訪問“ time_text”,而在類“ time_next”中進行設置時,這是一個錯字,但是即使我修復了它,我也必須在kv文件中為您調用的所有變量(嗡嗡聲,溫度)設置默認值,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語言文檔: 特殊語法下
我還無法運行您的代碼,我沒有使用的字體,因此我只注釋了這些行,一旦運行,我注意到Label顯示了標記,要使用它,您需要顯式設置標記=真
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'
最后,單擊“刷新”按鈕將調用沒有任何值的重新加載,而它需要一些值,因此“刷新”按鈕將引發TypeError。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.