我有一个类级布尔变量,它与所有的类和widget对话,每个类中的widget会根据这个值是否为真而改变。当我尝试执行时,Opacity抛出了以下错误。我已经尝试过AsyncImages,但它给出了一个加载标志,我试图避免。
以下是代码的片段
Python文件。
class MainScreen(Screen):
pass
class SecondScreen(Screen):
pass
class TestApp(App):
ABS_OPACITY=BooleanProperty(True)
def test(self):
print "Hello"
def build(self):
return presentation
Kivy文件 -
ScreenManagement:
MainScreen:
SecondScreen:
<MainScreen>:
name: "Main"
FloatLayout:
Button:
font_size:12
size_hint: 0.07, 0.05
text: "AC"
on_click:app.ABS_OPACITY=not app.ABS_OPACITY
pos_hint: {"right":0.93,"left":0.30, "bottom":0.1, "top": 0.93}
Image:
source: "xyz/image.png"
pos_hint:{"top":0.955}
opacity:1 if app.ABS_OPACITY else 0
Image:
source: "abc/image.png"
pos_hint:{"top":0.955}
opacity:0 if app.ABS_OPACITY else 1
<SecondScreen>:
name: "Second"
FloatLayout:
Button:
font_size:12
size_hint: 0.07, 0.05
text: "AC"
on_click:on_click:app.ABS_OPACITY=not app.ABS_OPACITY
pos_hint: {"right":0.93,"left":0.30, "bottom":0.1, "top": 0.93}
Image:
source: "xyz/image"
pos_hint:{"top":0.955}
opacity:1 if app.ABS_OPACITY else 0
Image:
source: "abc/image.png"
pos_hint:{"top":0.955}
opacity:0 if app.ABS_OPACITY else 1
以上是我在做同样的事情时面临的错误。
Traceback (most recent call last):
File "testapp.py", line 412, in <module>
presentation = Builder.load_file ("main.kv")
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 301, in load_file
return self.load_string(data, **kwargs)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 405, in load_string
rule_children=rule_children)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 657, in _apply_rule
root=rctx['ids']['root'], rule_children=rule_children)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/uix/widget.py", line 469, in apply_class_lang_rules
rule_children=rule_children)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 538, in apply
rule_children=rule_children)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 707, in _apply_rule
e), cause=tb)
kivy.lang.builder.BuilderException: Parser: File "/home/pi/Downloads/motormind/main.kv", line 161:
...
159: source: "UI_UX/blue/mainpage/abs.png"
160: pos_hint:{"top":0.955}
>> 161: opacity:0 if app.ABS_OPACITY else 1
162: #Park Lights On
163: Image:
...
BuilderException: Parser: File "/home/pi/Downloads/motormind/main.kv", line 161:
...
159: source: "UI_UX/blue/mainpage/abs.png"
160: pos_hint:{"top":0.955}
>> 161: opacity:0 if app.ABS_OPACITY else 1
162: #Park Lights On
163: Image:
...
AttributeError: 'NoneType' object has no attribute 'bind'
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 249, in create_handler
return eval(value, idmap), bound_list
File "/home/pi/Downloads/motormind/main.kv", line 161, in <module>
opacity:0 if app.ABS_OPACITY else 1
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/parser.py", line 75, in __getattribute__
object.__getattribute__(self, '_ensure_app')()
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/parser.py", line 70, in _ensure_app
app.bind(on_stop=lambda instance:
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 692, in _apply_rule
rctx['ids'])
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 254, in create_handler
cause=tb
这个错误是典型的调用 Builder.load_file()
对于 kv
文件,创建Widgets并引用了 app
之前 App
被定义。试着调用 Builder.load_file()
在 build()
的方法 App
.
解决这个问题的另一个方法是使用 <ScreenManagerment>
规矩 kv
文件,然后做
return ScreenManagement()
在你 build()
方法。