AttributeError:' super'对象没有属性' __ getattr __'在Kivy中将BoxLayout与多个kv文件一起使用时出错 [英] AttributeError: 'super' object has no attribute '__getattr__' Error when using BoxLayout with several kv-files in Kivy

查看:69
本文介绍了AttributeError:' super'对象没有属性' __ getattr __'在Kivy中将BoxLayout与多个kv文件一起使用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,这个问题已经被问过好几次了.但是尝试以下解决方案后:

As I am very well aware, this question has been asked several times already. But after trying the following solutions:

我得出的结论是,在我的特定问题上我需要帮助.列出的解决方案似乎不适用于我的具体情况.

I have reached the conclusion that i need help in my specific problem. The solutions listed didn't seem to work in my specific case.

以下情况:

我目前正在尝试使用kivy为智能手机开发应用程序.由于我喜欢干净整洁的结构代码,因此将Kivy代码拆分为几个kv文件.python代码应该主要具有逻辑,仅此而已.为了使其正常工作,我需要在不同的kv文件中引用不同对象的实例.为了弄清楚我的问题,我构建了一个相当简单的示例:

I am currently trying to develop an application for smartphones using kivy. Since i like my code pretty clean and clear structured i have split my Kivy code into several kv-files. The python code is supposed to have primarily the logic and nothing more. In order to get it working properly i need to reference the instances of the different objects in the different kv-files. In order to make my problem clear i have constructed a fairly simple example:

文件:try.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.factory import Factory
from kivy.uix.label import Label
from kivy.lang import Builder

x= 1

class ComplexBox(Widget):
    def testit(self):
        self.ids.layout.add_widget(Label(text = "Requirement A met."))
    def addsome(self):
        global x
        self.ids.layout.add_widget(SomeWidget(id="XXX"+str(x)))
        x = x +1
    pass

class SomeWidget(Widget):
    def change(self):
        self.ids.REQB.text = "Requirement B met."
    pass

class RequirementC(Widget):
    def triggerC(self):
        self.ids.ERRORBUTTON.text = "Requirement C met"
    pass

class Attempt(App):
    def build(self):
        return presentation
    pass


presentation = Builder.load_file("attempt.kv")
Attempt().run()

文件:try.kv

#:kivy 1.0
#:include attemptsupp.kv
#:include attemptsuppC.kv

# root
<ComplexBox>:
    BoxLayout:
        id: layout
        size: root.size
        Button:
            id: ERRORBUTTON
            text: "add"
            on_press: root.addsome()
            on_release: root.testit()
BoxLayout:
    orientation: 'vertical'
    ComplexBox:
    RequirementC:

文件:trysupp.kv

FILE: attemptsupp.kv

#:kivy 1.0

# rules for the widget
<SomeWidget>:
    BoxLayout:
        pos: root.pos
        size: root.size
        orientation: "vertical"
        Label:
            id: REQB
            text: "hello"
        Button:
            text: "world"
            on_release: root.change()

文件:trysuppC.kv

FILE: attemptsuppC.kv

#:kivy 1.0

<RequirementC>:
    Button:
        id: REQC
        text: "Press"
        on_release: root.triggerC()

正在运行的程序的图片-按下"Press"-按钮以获取错误

以kivy版本1.10和Python版本3.7.2运行,程序首先可以完美启动.但是,当我按下ID为ERRORBUTTON的标有"press"的按钮时,会出现此错误:

Running with kivy version 1.10 and Python version 3.7.2 the program first starts perfectly fine. But when I press on the Button labeled "press" with the id ERRORBUTTON I get this error:

...--default --nodebug --client --host localhost --port 57777...\attempt.py "
[INFO   ] [Logger      ] Record log in...\.kivy\logs\kivy_19-03-15_31.txt
[INFO   ] [Kivy        ] v1.10.1
[INFO   ] [Python      ] v3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 
...
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[WARNING] [Lang        ] attemptsupp.kv has already been included!
[WARNING] [Lang        ] attemptsuppC.kv has already been included!
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "kivy\properties.pyx", line 838, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'ERRORBUTTON'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "...\ptvsd_launcher.py", line 45, in <module>
     main(ptvsdArgs)
   ...
   File "e:\Daten\Github_Projects\pc-clicker\attempt.py", line 35, in <module>
     Attempt().run()
   File "...\lib\site-packages\kivy\app.py", line 826, in run
     runTouchApp()
...
   File ...\lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File ...\attemptsuppC.kv", line 7, in <module>
     on_release: root.triggerC()
   File "...\attempt.py", line 25, in triggerC
     self.ids.ERRORBUTTON.text = "Requirement C met"
   File "kivy\properties.pyx", line 841, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

即使我缩短了错误消息,也应该清楚会发生什么情况.我在RequirementC类中引用的ERRORBUTTON ID在词典中找不到.现在我的问题:

Even though I shortened the error message it should be clear what happens. The ERRORBUTTON id I am referencing in the RequirementC Class can't be found in the dictionary. Now to my question:

我如何使其起作用?我想念什么?

How can i make it work? What am I a missing?

简而言之,我尝试了一些事情:

Here in short a few things i have tried:

  • 我尝试将BoxLayouts包装在Screen中,并通过screenmanager访问它们.
  • 我尝试重新排列python代码中的顺序.(例如,首先加载主要kv文件)
  • 我尝试使用Builder Factory并在其中注册不同的类.
  • 我尝试更改参考.(例如self.ids.['ERRORBUTTON'] ...)

在我看来,这些尝试都没有奏效.

None of these attempts seem to have worked in my case.

所以总结一下:

我如何才能使跨不同类的kivy引用正常工作,为什么ERRORBUTTON ID不在我正在研究的字典中?

How can I get my kivy References across different classes to work properly and why is the ERRORBUTTON id not in the dict I am looking into?

推荐答案

因此,经过一番研究,我找到了所需的答案.对于那些可能也会在这里遇到我的问题的人来说,它来了:

So after a bit of research i found the answer i was looking for. For those who might also stumble over my problem here it comes:

class RequirementC(Widget):
def triggerC(self):
    presentation.children[1].ids.ERRORBUTTON.text = "Requirement C met"
pass

通过遍历演示文稿的子级,可以使用"ids"方法.

By going over the children of the presentation it is possible to use the "ids" method.

但是对于那些现在想使用它的人,我建议遍历孩子以找到正确的ID,而不是给出硬"引用(children [1]).

But for those tempted to use it now i would recommend iterating over the children to find the correct id instead of giving a "hard" reference (children[1]).

这篇关于AttributeError:&amp;#39; super&amp;#39;对象没有属性&amp;#39; __ getattr __&amp;#39;在Kivy中将BoxLayout与多个kv文件一起使用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆