简体   繁体   中英

Kivy KV File Custom Property

I cannot for the life of me figure out how to pass a custom property on a custom widget via the KV file. My application is a simple grid that contains a Button() and TestWidget(). TestWidget() has a StringProperty() test_property that doesn't seem to get the data from the KV file as seen by the print statement on init. Here's some quick straight forward code as an example.

Thanks.

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.properties import StringProperty


Builder.load_string("""
<TestWidget>:

<TestGrid>:
    Button:
    TestWidget:
        test_property: 'Test Property'
""")


class TestWidget(Widget):
    test_property = StringProperty()

    def __init__(self, **kwargs):
        super(TestWidget, self).__init__(**kwargs)

        print('Test OUTPUT:', self.test_property)


class TestGrid(GridLayout):
    pass


class MyApp(App):
    def build(self):
        return TestGrid()


MyApp().run()

I think I figured it out. Kivy doesn't pass anything to the objects. I learned this at https://kivy.org/docs/api-kivy.properties.html .

I use the on_ to do what needs to be done. There is a big difference between Kivy Objects and Python Objects.

Here's an example of a custom BoxLayout;

class KivyInput(BoxLayout):
    text_test = StringProperty()

    def __init__(self, **kwargs):
        super(KivyInput, self).__init__(**kwargs)

        self.orientation = 'horizontal'
        self.label = Label()
        self.text_input = TextInput(multiline=False)
        self.add_widget(self.label)
        self.add_widget(self.text_input)

    def on_text_test(self, instance, value):
        self.label.text = value

    def remove(self):
        self.clear_widgets()

Try printing it on the upcoming frame, instead of in the initiation of the object.
After the object is created, you can access the properties.
You do that with Clock. Like this:

from kivy.clock import Clock    

class TestWidget(Widget):
    test_property = StringProperty()

    def __init__(self, **kwargs):
        super(TestWidget, self).__init__(**kwargs)
        Clock.schedule_once(self.after_init) # run method on next frame

    def after_init(self,dt):
        print('Test OUTPUT:', self.test_property)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM