Kivy: how to instantiate a dynamic classes in python

I'm having a hard time trying to figure out how to instantiate a dynamic class I created using kv lang on my python code, consider the following code:

My test.kv file looks like this:

<MyPopup@Popup>:
    title:'hello'
    size_hint:(1, .6)
    GridLayout:
        id:root_grid
        cols:2
        padding:['8dp', '4dp','8dp','4dp']
        spacing:'8dp'
        Label:
            text:'some text here'
        Button:
            text:'Ok'
            on_press:do_something()
<MyGrid>:
    rows:1
    Button:
        text:'Show Popup'
        on_press:root.pop.show()

Then in my test.py:

from kivy.app               import App
from kivy.uix.gridlayout    import GridLayout
from kivy.uix.floatlayout   import FloatLayout
from kivy.uix.popup         import Popup
from kivy.factory           import Factory

class MyGrid(GridLayout):
    pop = Factory.MyPopup()
    pass

class Test(App):
    def build(self):
        return MyGrid()

if __name__=='__main__':
    Test().run()

The I get the following error: raise FactoryException('Unkown class <%s>' % name) kivy.factory.FactoryException: Unkown class

Can somebody please explain me how to properly do it, what am I missing? If you need any more information please let me know. Thanks.

Upvotes: 1

Views: 1795

Answers (1)

inclement
inclement

Reputation: 29450

Your call to the Factory takes place before the kv file is loaded, therefore the class you want does not yet exist.

Unless there is some reason to need a class level attribute, set self.pop in the __init__ of MyGrid instead.

You could also just include a Python class declaration. I generally prefer to do this for anything that I want to interact with from python, though opinions vary.

Upvotes: 1

Related Questions