Search code examples
pythonkivykivy-language

Kivy function as a property


I am working on a big GUI and inside the GUI, I might have the following Widget more often:

enter image description here

Therefor, I've made the following kivy class:

<ParameterFileInput@ParameterInput>
    ParameterLabel:
        width: root.label_width
        text: root.label_text
    FloatLayout:
        size_hint: None, 1.0
        width: 10
    ParameterTextInput:
        gid: root.content_id
        on_text_validate: app.set_widget_content(self.gid, None)

        size_hint: 0.7,1.0
        text: root.preset_text
    FloatLayout:
        size_hint: None, 1.0
        width: 10
    Button:
        text: "Browse"
        size_hint: None, 1.0
        on_press: root.button_function
        width: 100

and in python:

class ParameterInput(StackLayout):
    content_id = StringProperty()
    label_text = StringProperty()
    preset_text = StringProperty()
    label_width = NumericProperty(80)
    spinner_width = NumericProperty(100)
    spinner_values = ListProperty()
    button_function = ObjectProperty()

The thing is the button_function that should be called when I hit the button. On example of my implementation is the following:

ParameterFileInput:
    label_text: "data_file"
    content_id: "data_file"
    button_function: app.train_button()

Probably needless to say that app.train_button() is not called.

I would be very happy if someone could help me with this.

Greetings, Finn


Solution

  • The problem is that in the bind in python you pass the name of the function, internally kivy makes the invocation of the function, in the case of the kivy does not, so you must make the invocation, in your case the solution is:

    on_press: root.button_function()