4

I have this code and it works, but I'm sure it's not the way to go properly. In my .kv a button fires two functions in my main.py.

    Button:
    text: "Press Me"
    on_release: root.on_our_btn_release(text_input.text)
    on_release: root.get_items(text_input.text)

Next step in my awesome project will be adding a lot more functions that will have to go off when the same button is clicked. This will result in a rather long list like:

    Button:
    text: "Press Me"
    on_release: root.on_our_btn_release(text_input.text)
    on_release: root.get_items(text_input.text)
    on_release: root.another_function(text_input.text)
    on_release: root.andanotherone(text_input.text)
    on_release: root.herewegoagain(text_input.text)
    on_release: root.this_is_getting_boring(text_input.text)
    on_release: root.think_you_got_the_picture(text_input.text)

This looks to me as very ugly code, but I didn't find a pretty way to do this yet.

Anyone? Thanks in advance!

;-) Erik

Erik van Elten
  • 405
  • 1
  • 4
  • 10

3 Answers3

3

You could structure this in a number of different ways, and the best option is mostly up to you.

One option if you don't want too many functions in kv is to call a single root.do_everything(), and put all the other calls in that on the python side.

inclement
  • 29,124
  • 4
  • 48
  • 60
  • Thank you for your reply. I already guessed (and found on Google) that there are numerous ways to fix this, but I like to learn what would be the most efficient way. – Erik van Elten May 12 '16 at 10:34
1

I believe a slightly more elegant solution would be to indent and list the different callbacks.

on_release:
    first()
    second()

Another possible but ugly solution for this can be to separate the functions with a semicolon.

on_release: first(); second()
SomeMosa
  • 413
  • 5
  • 28
0

You could make use of "on_press" and "on_release". Assign one method/function to "on_press" and the other to "on_release"

Derek
  • 11
  • 5