【发布时间】:2019-03-15 23:27:55
【问题描述】:
我很清楚,这个问题已经被问过好几次了。但是在尝试了以下解决方案之后:
- Python - Kivy: AttributeError: 'super' object has no attribute '__getattr__' when trying to get self.ids
- Python/Kivy AttributeError: 'super' object has no attribute '__getattr__'
- How can i access object properties of another class in kivy without getting AttributeError: 'super' object has no attribute '__getattr__'
- AttributeError: 'super' object has no attribute '__getattr__'
我已经得出结论,我需要帮助解决我的具体问题。列出的解决方案似乎不适用于我的具体情况。
以下情况:
我目前正在尝试使用 kivy 为智能手机开发应用程序。因为我喜欢我的代码非常干净和清晰的结构,所以我将我的 Kivy 代码拆分为几个 kv 文件。 python 代码应该主要具有逻辑,仅此而已。为了让它正常工作,我需要在不同的 kv 文件中引用不同对象的实例。为了让我的问题更清楚,我构建了一个相当简单的例子:
文件:尝试.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()
文件:尝试.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:
文件: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()
文件:attemptsuppC.kv
#:kivy 1.0
<RequirementC>:
Button:
id: REQC
text: "Press"
on_release: root.triggerC()
picture of the Running Program - press the "Press"- button to get the error
使用 kivy 1.10 版和 Python 3.7.2 版运行,该程序首先可以正常启动。但是,当我按下标有“按下”且 ID 为 ERRORBUTTON 的按钮时,出现此错误:
...--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。现在回答我的问题:
我怎样才能让它工作?我缺少什么?
简而言之,我尝试了几件事:
- 我已尝试将 BoxLayouts 包装在 Screen 中并通过 screenmanager 访问它们。
- 我已经尝试在 python 代码中重新排列顺序。 (例如先加载主kv文件)
- 我已尝试使用 Builder Factory 并在那里注册不同的类。
- 我已尝试更改参考。 (例如 self.ids.['ERRORBUTTON']...)
这些尝试对我来说似乎都没有奏效。
总结一下:
如何让不同类的 kivy 引用正常工作,为什么 ERRORBUTTON id 不在我正在查看的字典中?
【问题讨论】:
标签: python kivy kivy-language