【问题标题】:Kivy returns "AttributeError: 'NoneType' object has no attribute 'bind'" when accessing ListPropertyKivy 在访问 ListProperty 时返回“AttributeError: 'NoneType' 对象没有属性 'bind'”
【发布时间】:2016-03-26 20:34:22
【问题描述】:

我正在尝试创建一个显示图像文件的屏幕,该图像文件的路径存储在 ListProperty 中。我了解错误消息表明 Kivy 正在尝试在创建 ListProperty 之前访问该值,但我不知道如何解决此问题。

这是我的 main.py 脚本中的一个 sn-p,其中属性被初始化为一个包含单个空字符串的空列表,并且调用了 build 方法:

presentation = Builder.load_file("main.kv")

class MainApp(App):
    image_list = ListProperty([''])

    def build(self):
        return presentation

if __name__ == '__main__':
    MainApp().run()

这是 main.kv 中使用该属性的部分:

<Screen1>:
    name: 'screen1'
    BoxLayout:
        orientation: 'horizontal'
        Picture:
            source: app.image_string.pop()

抛出的异常如下:

 ...
 BuilderException: Parser: File "main.kv", line 71:
 ...
      69:        orientation: 'horizontal'
      70:        Picture:
 >>   71:            source: app.image_string.pop()
      72:

任何有关如何解决此问题的指导将不胜感激。谢谢!

EDIT 读者 FIns 指出我调用的是 image_string 而不是 image_list,但即使在进行更正后,我也会收到同样的错误:

BoxLayout:
    orientation: 'horizontal'
    Picture:
        source: app.image_list.pop()
 BuilderException: Parser: File "main.kv", line 71:

还有……

 BuilderException: Parser: File "main.kv", line 71:
 ...
      69:        orientation: 'horizontal'
      70:        Picture:
 >>   71:            source: app.image_list.pop()

【问题讨论】:

  • 在您声明image_list的应用程序中,但您在main.kv中使用了image_string
  • 哎呀!你说得对。我正在试验 ListProperty 和 StringProperty 但这仍然不能解决我的问题。即使我纠正了该语法错误,我也会得到相同的行为。

标签: python python-2.7 kivy listproperty


【解决方案1】:

在 build 方法中加载 kivy 设计语言在此示例中有效:

from kivy.app import App 
from kivy.properties import ListProperty 
from kivy.base import Builder

class MainApp(App):
    image_list = ListProperty([''])

    def build(self):
        presentation = Builder.load_string(""" 
Screen:
    name: 'screen1'
    BoxLayout:
        Image:
            source: app.image_list.pop()
    """)

        return presentation

if __name__ == '__main__':
    MainApp().run()

【讨论】:

  • 感谢 Flns!我会试一试。我想我宁愿只维护一个文件,所以使用构建字符串可能是最好的方法。
  • 感谢一百万个 FIN!我已经为此拉了两天的头发,您的解决方案效果很好!非常感谢您在这方面为我提供帮助。
猜你喜欢
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多