Kivy: AttributeError: 'Label' object has no attribute 'a'
Kivy: AttributeError: 'Label' object has no attribute 'a'
我制作了一个简单的应用程序,其中有两个计时器同时 运行。一个向上计数,一个向下计数。
我最初尝试在Label下缩进声明"text: str(round(self.a, 1))",结果会出现标题中所述的错误。我现在已经通过如下所示调整我的代码解决了这个问题(更改是在最后的 .kv 文件部分中进行的):
from kivy.app import App
from kivy.uix.label import Label
from kivy.animation import Animation
from kivy.properties import NumericProperty
from random import randint
from kivy.uix.boxlayout import BoxLayout
class PleaseWork(BoxLayout):
a = NumericProperty(randint(3,7))
b = NumericProperty(0)
def start(self):
self.anim = Animation(a=0, duration=self.a)
self.anim &= Animation(b=15, duration=15)
self.anim.repeat = True
self.anim.start(self)
class PleaseApp(App):
def build(self):
p = PleaseWork()
p.start()
return p
if __name__ == "__main__":
PleaseApp().run()
<PleaseWork>
orientation: 'vertical'
text_1: str(round(self.a, 1))
text_2: str(round(self.b, 1))
Label:
text: root.text_1
Label:
id: count_up
text: root.text_2
虽然代码现在执行了它应该执行的操作,但我的问题是为什么这会纠正错误?我真的不明白为什么这会有所不同?
问题是变量的作用域,在.kv中至少有以下几种方式来访问一个元素:
- id
:
<A>:
id: a
property_a: b.foo_property
<B>:
id: b
property_b: a.bar_property
它用于引用树中的任何节点。
- self
:
<A>:
property_a: self.foo_property
B:
property_b: self.bar_property
当使用self
时,表示同一个节点引用自身,在前面的例子property_b: self.bar_property
中指出property_b
的属性 ofb
将采用与 bar_property
ofb
相同的值。它与 python 类.
中的用法相同
- root
:
<A>:
B:
property_b: root.bar_property
<C>:
D:
property_d: root.bar_property
引用树根时使用root
,例如property_b: root.bar_property
表示b
的property_b
将取与[=21=相同的值] 来自a
。在 property_d: root.bar_property
的情况下,它表示 d
的 property_d
将与 c
的 bar_property
具有相同的值。
综合以上,以下也是解决方案:
1.
<PleaseWork>
orientation: 'vertical'
Label:
text: str(round(root.a, 1))
Label:
id: count_up
text: str(round(root.b, 1))
2.
<PleaseWork>
orientation: 'vertical'
id: please_work
Label:
text: str(round(please_work.a, 1))
Label:
id: count_up
text: str(round(please_work.b, 1))
我制作了一个简单的应用程序,其中有两个计时器同时 运行。一个向上计数,一个向下计数。
我最初尝试在Label下缩进声明"text: str(round(self.a, 1))",结果会出现标题中所述的错误。我现在已经通过如下所示调整我的代码解决了这个问题(更改是在最后的 .kv 文件部分中进行的):
from kivy.app import App
from kivy.uix.label import Label
from kivy.animation import Animation
from kivy.properties import NumericProperty
from random import randint
from kivy.uix.boxlayout import BoxLayout
class PleaseWork(BoxLayout):
a = NumericProperty(randint(3,7))
b = NumericProperty(0)
def start(self):
self.anim = Animation(a=0, duration=self.a)
self.anim &= Animation(b=15, duration=15)
self.anim.repeat = True
self.anim.start(self)
class PleaseApp(App):
def build(self):
p = PleaseWork()
p.start()
return p
if __name__ == "__main__":
PleaseApp().run()
<PleaseWork>
orientation: 'vertical'
text_1: str(round(self.a, 1))
text_2: str(round(self.b, 1))
Label:
text: root.text_1
Label:
id: count_up
text: root.text_2
虽然代码现在执行了它应该执行的操作,但我的问题是为什么这会纠正错误?我真的不明白为什么这会有所不同?
问题是变量的作用域,在.kv中至少有以下几种方式来访问一个元素:
- id
:
<A>:
id: a
property_a: b.foo_property
<B>:
id: b
property_b: a.bar_property
它用于引用树中的任何节点。
- self
:
<A>:
property_a: self.foo_property
B:
property_b: self.bar_property
当使用self
时,表示同一个节点引用自身,在前面的例子property_b: self.bar_property
中指出property_b
的属性 ofb
将采用与 bar_property
ofb
相同的值。它与 python 类.
- root
:
<A>:
B:
property_b: root.bar_property
<C>:
D:
property_d: root.bar_property
引用树根时使用root
,例如property_b: root.bar_property
表示b
的property_b
将取与[=21=相同的值] 来自a
。在 property_d: root.bar_property
的情况下,它表示 d
的 property_d
将与 c
的 bar_property
具有相同的值。
综合以上,以下也是解决方案:
1.
<PleaseWork>
orientation: 'vertical'
Label:
text: str(round(root.a, 1))
Label:
id: count_up
text: str(round(root.b, 1))
2.
<PleaseWork>
orientation: 'vertical'
id: please_work
Label:
text: str(round(please_work.a, 1))
Label:
id: count_up
text: str(round(please_work.b, 1))