我试图将方法绑定到微调器的文本值。它必须在不晚于显示TestScreen时绑定。如果我不使用ScreenManager,这是有效的(例如,如果TestApp.build返回TestScreen而不是TestScreenManager)。当TestApp.build返回TestScreenManager时,我在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方法中有效。
目前,每次按下微调器时,我都可以通过绑定函数来找到解决方法。但这似乎不是解决问题的最佳方法
on_press: self.bind(text=root.on_spinner_select)
所以我的问题是:在使用ScreenManager时如何在加载时将方法绑定到微调器?
答案 0 :(得分:0)
尝试使用__init__(self, *args, **kwargs)
。
使用** kwargs只假设输入总是某种关键字参数(例如,dicts)。这几乎就是错误的含义,因为它试图从您传递的参数中获取所有属性。
答案 1 :(得分:0)
当您尝试绑定此方法时,我猜您的屏幕初始化尚未完成。试试这个:
...
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)