問題描述
問題:什么是沒有屬性已構(gòu)建"錯誤,我需要做什么來更正此代碼,以便它可以接收日期時間對象并顯示倒計時?抱歉,帖子太長了.
Question: What is a 'has no attribute 'built' error, and what do I need to do to correct this code so that it can take in a datetime object and display the count down? Sorry for the long post.
我已經(jīng)提供了代碼和 .kv 文件的鏈接.
I've provided the code and a link to the .kv file.
我嘗試創(chuàng)建一個倒計時時鐘,它將 datetime 對象作為參數(shù)并倒計時到該日期(使用 python 和 kivy).它基本上是對 Adam Giermanowski 的倒計時教程.
I tried to create a countdown clock that takes a datetime object as a parameter and counts down to that date (using python and kivy). It's basically an slight adaptation of Adam Giermanowski's countdown timer tutorial.
這是我的代碼:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.clock import Clock
import datetime
#datetime object
b= datetime.datetime(2016,9,12,3,5)
class Counter_Timer(BoxLayout):
days = StringProperty()
hours = StringProperty()
minutes = StringProperty()
seconds = StringProperty()
def __init__(self, datetimeOBJ):
self.datetimeOBJ = datetimeOBJ
def update(self, dt):
#the difference in time
delta = self.datetimeOBJ - datetime.datetime.now()
self.days = str(delta.days)
hour_string = str(delta).split(', ')[1]
self.hours = hour_string.split(':')[0]
self.minutes = hour_string.split(':')[1]
self.seconds = hour_string.split(':')[2].split('.')[0]
class Counter(App):
#takes a datetime object as a parameter
def __init__(self, datetimeOBJ):
self.datetimeOBJ = datetimeOBJ
def build(self):
Counter = Counter_Timer(self.datetimeOBJ)
Clock.schedule_interval(Counter.update, 1.0)
return Counter
if __name__=='__main__':
Counter(b).run()
這是 Counter(b).run() 行的錯誤:
Here's the error on the Counter(b).run() line:
AttributeError: 'Counter' object has no attribute 'built'
推薦答案
你必須在重寫 __init__
時調(diào)用超類的構(gòu)造函數(shù),這樣構(gòu)造函數(shù)所做的所有事情才能擁有課堂作業(yè)的其他方法已完成.你的 init 方法應該是這樣的:
You have to call the superclasses constructor when you override __init__
, so that all of the things that that constructor does in order to have the other methods of the class work gets done. Your init method should be this:
def __init__(self, datetimeOBJ):
App.init(self)
self.datetimeOBJ = datetimeOBJ
這篇關于Kivy/Python Countdown App 項目 kivy 沒有屬性 'built' 錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!