我在KV语言中有以下内容(简化示例): 我的问题是最后一行(on_release)。
#:import Factory kivy.factory.Factory
<MyCustomWidgets>:
ListView:
id: my_listview
<MainScreen>:
Button:
text: "Choose File"
on_release: Factory.FileChooserDialog().open()
MyCustomWidgets:
<FileChooserDialog@ModalView>:
FileChooserIconView:
id: filechooser
Button:
text: "OK"
on_release: app.root.add_to_listview("Sample Text", app.root.ids.my_listview)
在Python中,我有:
class MainScreen(BoxLayout):
def add_to_listview(self, thelistview):
# For testing purposes.
print(type(thelistview))
在KV中,在最后一行,我正在尝试运行一个python方法,该方法将一个字符串添加到id为“my_listview”的listview。
我收到此错误:
AttributeError:'super'对象没有属性' getattr '
答案 0 :(得分:2)
使用Kivy ObjectProperty ,my_listview = ObjectProperty(None)
并将其(my_listview: my_listview
)连接到kv文件中定义的id: my_listview
。有关详细信息,请参阅示例和输出。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class MyCustomWidgets(BoxLayout):
my_listview = ObjectProperty(None)
class MainScreen(BoxLayout):
def add_to_listview(self, *args, thelistview):
# For testing purpose
print(self)
print(args[0])
print(thelistview)
class TestApp(App):
title = "Reference widget using id in Kivy (kv language)"
def build(self):
return MainScreen()
if __name__ == "__main__":
TestApp().run()
#:kivy 1.10.0
#:import Factory kivy.factory.Factory
<MyCustomWidgets>:
my_listview: my_listview
ListView:
id: my_listview
<MainScreen>:
orientation: "vertical"
Button:
text: "Choose File"
on_release: Factory.FileChooserDialog().open()
MyCustomWidgets:
id: my_cw
<FileChooserDialog@ModalView>:
id: filechooser
Button:
text: "OK"
on_release:
app.root.add_to_listview("Sample Text", thelistview=app.root.ids.my_cw.my_listview)