[英]how do i solve the attribute error in kivy?
Hi i am new to kivy and i am getting attribute error much though i think i had placed the good place of coding .嗨,我是 kivy 的新手,虽然我认为我已经放置了编码的好地方,但我经常遇到属性错误。 Here is my code
这是我的代码
from kivy.uix.widget import Widget
class widget_example(GridLayout):
def button_click(self):
print('button clicked')
class MainWidget(Widget):
pass
class thelabapp(App):
pass
if __name__ == '__main__':
thelabapp().run()
and the .kv file contains并且 .kv 文件包含
<widget_example>:
cls: 3
Button:
text:"Click here"
on_press:root.button_click()
Label:
text:'Hello world'
and the error arise as并且错误出现为
File "g:\project\game\kivy2\main.py", line 54, in <module>
thelabapp().run()
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py", line
949, in run
self._run_prepare()
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py", line
918, in _run_prepare
self.load_kv(filename=self.kv_file)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py", line
691, in load_kv
root = Builder.load_file(rfilename)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.py", line 306, in load_file
return self.load_string(data, **kwargs)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.p root=widget, rule_children=rule_children)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 465, in apply_class_lang_rules
rule_children=rule_children)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.py", line 543, in apply
rule_children=rule_children)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.py", line 621, in _apply_rule
cls = Factory_get(cname)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\factory.py", line 145, in __getattr__
'First letter of class name <%s> is in lowercase' % name)
AttributeError: First letter of class name <anchorlayout> is in lowercase
thanks in advance提前致谢
The problem is that kv
expects class names to be capitalized.问题是
kv
期望类名大写。 See the documentation .请参阅文档。
So try changing your kv to:因此,尝试将您的 kv 更改为:
Widget_example:
<Widget_example>:
cols: 3
Button:
text:"Click here"
on_press:root.button_click()
Label:
text:'Hello world'
In python file:在python文件中:
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.app import App
class Widget_example(GridLayout):
def button_click(self):
print('button clicked')
class MainWidget(Widget):
pass
class thelabapp(App):
pass
if __name__ == '__main__':
thelabapp().run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.