Kivy: Bind Multiple Functions to a Single Button

Rachela
Jul 21, 2021

Today I learned that you can have one button execute multiple functions. Here is how:

.kv file: just list the functions one after another. No commas needed. root.add_info() refers to the add_info() function that I created in my .py file. In this case, the button executes add_info() then changes to the home screen by sliding to the right.

Button:
text: "Add"
on_press:
root.add_info()
root.manager.transition.direction = "right"
root.manager.current = "home_screen"
size_hint: 0.6, 0.6

.py file: I used .bind() twice on the same button.

update_info = Button(text="Yes", size_hint=(0.1, 0.1))

update_info.bind(on_press=lambda x: self.add_update(name))
update_info.bind(on_press=new_popup.dismiss)

This is an example from my code where I updated the name information in a database and then dismissed the popup. I used “lambda x:” to ensure that the self.add_update(name) function would not execute until the button is pressed. If you want to learn more about lambdas, click here: https://www.w3schools.com/python/python_lambda.asp

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Rachela
Rachela

Written by Rachela

I am a librarian and new computer programmer creating an app with Python and Kivy to document the plants in my garden.

No responses yet

Write a response