简体   繁体   中英

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 . 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

<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. See the documentation .

So try changing your kv to:

Widget_example:
<Widget_example>:
   cols: 3
   Button:
      text:"Click here"
      on_press:root.button_click()
   Label:
      text:'Hello world'

In python file:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM