user16836597
user16836597

Reputation:

Multiple actions on release in Kivy

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

Upvotes: 1

Views: 892

Answers (1)

hc_dev
hc_dev

Reputation: 9418

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

Upvotes: 1

Related Questions