1

Can I just put more function in this on release or this wont work. I want to have more pop functions and pictures displayed, maybe some audio, etc. This is .kv file:

<Root>:

    orientation: 'vertical'
    RecordButton:
        id: record_button
        background_color: 1,1.01,0.90,1
        text: 'Order'
        on_release: 
            self.record()
            root.pop1()
        height: '100dp'
        size_hint_y: None

    TextInput:
        text: record_button.output
        readonly: True
  • You can call a single function and put everything you want in that, or write multiple `on_release` rows (but maybe order of running is not always preserved in that case), or separate your function calls with semicolons, or maybe with the right indentation you can achieve what you're currently trying. – inclement Sep 20 '21 at 21:23

1 Answers1

1

Defining an event callback as sequence of statements.

Inside the KV file

Indentation and thus structuring control-flow readable is limited in a KV file. As inclement commented there are basically 2 ways for defining a callback sequence:

  • statements per line (same indentation)
  • semicolon separated statements
on_release: 
    self.record()
    root.pop1()
on_press: print('pressed'); self.insert_text("pressed!")

See Kivy-language syntax Valid expressions inside :

[..] multiple single line statements are valid, including those that escape their newline, as long as they don’t add an indentation level.

Define a function in Python

You have more flexibility to define the a function inside the Python script and declare this callback on the event inside the KV file.

For example the function as method of your RecordButton (assume its a class extending Button) in Python:

class RecordButton(Button):
    # callback function tells when button released
    # It tells the state and instance of button.
    def record_and_pop(self, instance):
        print("Button is released")
        print('The button <%s> state is <%s>' % (instance.text, instance.state))
       self.record()
       root.pop1()

then used inside the KV:

RecordButton:
    on_release: self.record_and_more()

See also

hc_dev
  • 8,389
  • 1
  • 26
  • 38