Kivy:无法引用 .kv 文件属性
Kivy: Cannot refer to .kv file attribute
我正在使用 Python 3.7 和 Kivy 1.10.1。我似乎无法弄清楚这一点。我正在尝试在 Kivy 中添加一个标签(最终是一个按钮)的动画。但我不断收到:
AttributeError: 'IntroScreen' object has no attribute 'lbl'
class IntroScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.animate()
def animate(self):
anim = Animation(opacity=0, duration=3)
anim.start(self.lbl)
class MainScreen(GridLayout, Screen):
pass
class AnotherScreen(GridLayout, Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("blank.kv")
class SimpleKivy(App):
def build(self):
self.title = "woods"
return presentation
if __name__ == '__main__':
SimpleKivy().run()
我的 .kv 文件的相关部分如下所示:
# File name: text_game.py
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
IntroScreen:
MainScreen:
AnotherScreen:
<CustButton@Button>:
font_size: 50
font_name: "silly"
color: 0,1,0,1
<IntroScreen>:
lbl: lbl
canvas.before:
Rectangle:
size: 20, 20
source: "cabin.png"
Label:
id: lbl
text: "Howdy"
如有任何帮助,我们将不胜感激。我不明白为什么它没有在 .kv 文件中找到 lbl 属性。提前致谢!
编译 .kv 时,所做的是在添加 .kv 中指示的属性之前使用 .py 中声明的基础 类,以便此时 animate()
方法将被调用,其中尝试使用 lbl 并且当时显然未定义,因此您抛出该错误。
所以在这些情况下,您必须在 .kv 添加其他属性后立即调用 animate()
,为此我们使用 Clock.schedule_once()
,如下所示:
from kivy.clock import Clock
class IntroScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(lambda *args: self.animate())
def animate(self):
anim = Animation(opacity=0, duration=3)
anim.start(self.lbl)
我正在使用 Python 3.7 和 Kivy 1.10.1。我似乎无法弄清楚这一点。我正在尝试在 Kivy 中添加一个标签(最终是一个按钮)的动画。但我不断收到:
AttributeError: 'IntroScreen' object has no attribute 'lbl'
class IntroScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.animate()
def animate(self):
anim = Animation(opacity=0, duration=3)
anim.start(self.lbl)
class MainScreen(GridLayout, Screen):
pass
class AnotherScreen(GridLayout, Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("blank.kv")
class SimpleKivy(App):
def build(self):
self.title = "woods"
return presentation
if __name__ == '__main__':
SimpleKivy().run()
我的 .kv 文件的相关部分如下所示:
# File name: text_game.py
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
IntroScreen:
MainScreen:
AnotherScreen:
<CustButton@Button>:
font_size: 50
font_name: "silly"
color: 0,1,0,1
<IntroScreen>:
lbl: lbl
canvas.before:
Rectangle:
size: 20, 20
source: "cabin.png"
Label:
id: lbl
text: "Howdy"
如有任何帮助,我们将不胜感激。我不明白为什么它没有在 .kv 文件中找到 lbl 属性。提前致谢!
编译 .kv 时,所做的是在添加 .kv 中指示的属性之前使用 .py 中声明的基础 类,以便此时 animate()
方法将被调用,其中尝试使用 lbl 并且当时显然未定义,因此您抛出该错误。
所以在这些情况下,您必须在 .kv 添加其他属性后立即调用 animate()
,为此我们使用 Clock.schedule_once()
,如下所示:
from kivy.clock import Clock
class IntroScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(lambda *args: self.animate())
def animate(self):
anim = Animation(opacity=0, duration=3)
anim.start(self.lbl)