【问题标题】:how do i solve the attribute error in kivy?如何解决kivy中的属性错误?
【发布时间】:2023-04-02 19:28:01
【问题描述】:

嗨,我是 kivy 的新手,虽然我认为我已经放置了编码的好地方,但我遇到了很多属性错误。 这是我的代码

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

.kv 文件包含

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

错误出现

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

提前致谢

【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

    问题是kv 期望类名大写。请参阅documentation

    所以尝试将您的 kv 更改为:

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

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

    【讨论】:

      猜你喜欢
      • 2013-11-18
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多