Kivy/Python Countdown App 项目 kivy 没有属性 'built' 错误
问题描述
问题:什么是没有属性已构建"错误,我需要做什么来更正此代码,以便它可以接收日期时间对象并显示倒计时?抱歉,帖子太长了.
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.
我已经提供了代码和 .kv 文件的链接.
I've provided the code and a link to the .kv file.
我尝试创建一个倒计时时钟,它将 datetime 对象作为参数并倒计时到该日期(使用 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__
时调用超类的构造函数,这样构造函数所做的所有事情才能拥有课堂作业的其他方法已完成.你的 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
相关文章