3

I have the following in KV language (simplified example): My issue is with the last line (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)

In Python, I have:

class MainScreen(BoxLayout):
    def add_to_listview(self, thelistview):
        # For testing purposes.
        print(type(thelistview))

In KV, on the last line, I'm trying to run a python method which adds a string to a ListView that has an id of my_listview.

I get this error:

AttributeError: 'super' object has no attribute '__getattr__'
garlic
  • 197
  • 1
  • 11
  • I don't understand the example, why are these rules indented? and why do they have id, it doesn't make sense for a rule to have an id, since ids are local to the rule… – Tshirtman Sep 24 '17 at 17:21
  • @Tshirtman: the idents was a mistake, I fixed the sample and only put the id's where it matters. I basically want to add the selected path/filename to the ListView (from filechooser), but I'm not sure how to reach the ListView. – garlic Sep 24 '17 at 18:51

1 Answers1

3

Use a Kivy ObjectProperty, my_listview = ObjectProperty(None) and hook it up (my_listview: my_listview) to the id: my_listview defined in the kv file. Please refer to the example and output for details.

Example

main.py

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()

test.kv

#: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)

Output

enter image description here

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • What if MyCustomWidgets: has a BoxLayout as a parent? In my real code, the ListView is inside a BoxLayout. How would I reference the ListView then? Example: class MyCustomWidgets(BoxLayout): pass – garlic Sep 25 '17 at 23:16
  • I have updated my example to reflect MyCustomWidgets class is a BoxLayout and ListView is a child widget inside the boxlayout. Please refer to my example for details in regards to referencing the ListView. – ikolim Sep 26 '17 at 15:12