Kivy AttributeError:“超级"对象在ScreenManager中没有属性"__getattr__" [英] Kivy AttributeError: 'super' object has no attribute '__getattr__' with ScreenManager
问题描述
我正在尝试将方法绑定到微调器的文本值.必须在显示TestScreen时绑定它.如果我不使用ScreenManager,则此方法有效(例如,如果TestApp.build返回TestScreen而不是TestScreenManager).当TestApp.build返回TestScreenManager时,当我在TestScreen.__init__
I am attempting to bind a method to the text value of a spinner. It needs to be bound no later than when the TestScreen is displayed. This works if I don't use the ScreenManager, (eg if TestApp.build returns TestScreen instead of TestScreenManager). When TestApp.build returns TestScreenManager I get the following error when I reference self.ids in TestScreen.__init__
AttributeError: 'super' object has no attribute '__getattr__'
Test.py
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.app import App
class TestScreen(Screen):
def __init__(self, *args, **kwargs):
super(TestScreen, self).__init__(*args, **kwargs)
self.ids.test_spinner.bind(text=self.on_spinner_select)
def on_spinner_select(self, instance, data, *largs):
print("In on_spinner_select")
def print_spinner_value(self):
print(self.ids.test_spinner.text)
class TestScreenManager(ScreenManager):
pass
class TestApp(App):
def build(self):
#return TestScreen()
return TestScreenManager()
if __name__ == "__main__":
TestApp().run()
Test.kv
<TestScreen>:
name: "World Screen"
BoxLayout:
orientation: 'vertical'
Label:
text: "Name"
font_size: 30
BoxLayout:
Label:
text: "Active Selection"
size_hint_x: .5
Spinner:
id: test_spinner
text: "Value1"
values: ["Value1", "Value2"]
Button:
text: "Print spinner value"
on_press: root.print_spinner_value()
<TestScreenManager>:
TestScreen:
我尝试将方法绑定到on_enter方法中,但是遇到相同的错误.但是,如果我在init函数中注释掉self.ids语句,则self.ids确实可以在方法print_spinner_value中起作用.
I have tried binding the method in the on_enter method but I get the same error. However, self.ids does work in the method print_spinner_value if I comment out the self.ids statement in the init function.
当前,每次按下微调器时,通过绑定功能,我都能找到解决方法.但这似乎不是解决问题的最佳方法
Currently I was able to find a work around by binding the function every time the spinner is pressed. But that doesn't seem like the best way to handle the problem
on_press: self.bind(text=root.on_spinner_select)
所以我的问题是:使用ScreenManager时如何在加载时将方法绑定到微调器?
So my question is: How do I bind a method to the spinner on load while using the ScreenManager?
推荐答案
当您尝试绑定此方法时,我猜您的屏幕初始化尚未完成.试试这个:
I guess the init of your screen hasn't finished when you try to bind this method. Try this:
...
from kivy.clock import Clock
...
class TestScreen(Screen):
def __init__(self, **kwargs):
super(TestScreen, self).__init__(**kwargs)
Clock.schedule_once(self.on_start)
def on_start(self, *args):
self.ids.test_spinner.bind(text=self.on_spinner_select)
这篇关于Kivy AttributeError:“超级"对象在ScreenManager中没有属性"__getattr__"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!