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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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() 

Comments

0

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

3 Comments

Could you share a code example on how to do it?
@SimasJoneliunas I believe they mean binding one function to on_press (ex. on_press: first()) and another one to on_release (ex. on_release: second())
yes I definitely meant what @Pokechu48 just stated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.