kivy AttributeError:'button'对象没有属性'background_color'

时间:2017-08-04 22:29:14

标签: python kivy kivy-language

实际上,我正在测试kivy,这是我刚刚安装的,我做了一个简单的程序来测试它,这是程序: -

from kivy.app import App
from kivy.uix.widget import Widget

class button(Widget):
    pass

class ButtonApp(App):
    def build(self):
        return button()

if __name__=="__main__":
    ButtonApp().run()

当我运行它时,我收到以下错误。我试图解决它并在谷歌搜索太多次但没有得到解决方案。请帮帮我。

 Traceback (most recent call last):
   File "/root/PycharmProjects/untitled/Button.py", line 12, in <module>
     ButtonApp().run()
   File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/app.py", line 802, in run
     root = self.build()
   File "/root/PycharmProjects/untitled/Button.py", line 9, in build
     return button()
   File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/uix/widget.py", line 345, in __init__
     Builder.apply(self, ignored_consts=self._kwargs_applied_init)
   File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/lang/builder.py", line 451, in apply
     self._apply_rule(widget, rule, rule, ignored_consts=ignored_consts)
   File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/lang/builder.py", line 506, in _apply_rule
     rule.canvas_root, rootrule)
   File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/lang/builder.py", line 831, in _build_canvas
     '{}: {}'.format(e.__class__.__name__, e), cause=tb)
 kivy.lang.builder.BuilderException: Parser: File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/data/style.kv", line 17:
 ...
      15:    canvas:
      16:        Color:
 >>   17:            rgba: self.background_color
      18:        BorderImage:
      19:            border: self.border
 ...
 BuilderException: Parser: File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/data/style.kv", line 17:
 ...
      15:    canvas:
      16:        Color:
 >>   17:            rgba: self.background_color
      18:        BorderImage:
      19:            border: self.border
 ...
 AttributeError: 'button' object has no attribute 'background_color'
   File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/lang/builder.py", line 249, in create_handler
     return eval(value, idmap), bound_list
   File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/data/style.kv", line 17, in <module>
     rgba: self.background_color
   File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__ (/tmp/pip-build-LU3E8I/kivy/kivy/weakproxy.c:1386)

   File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/lang/builder.py", line 825, in _build_canvas
     key, value, prule, idmap, True)
   File "/root/kivyinstall/local/lib/python2.7/site-packages/kivy/lang/builder.py", line 254, in create_handler
     cause=tb)


Process finished with exit code 1

1 个答案:

答案 0 :(得分:1)

已经有一个名为Button的Kivy小部件。由于kv语言中的大小写区分限制,您自己的button类与此相冲突。

一般情况下,你应该使用CamelCase来获取widget类名(与pep8一致),并避免复制Kivy的内置类名。这将确保一切正常。