I'd like to react to the event when the spinner
changes value and capture that value.
Here is the minimal reproducible example:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.spinner import Spinner
class MyApp(App):
def build(self):
mainbox = BoxLayout(orientation="horizontal",
spacing=15,
padding=5)
self.spinner_languages = Spinner(text ="",
values =("Python", "Java", "C++", "C", "C#", "PHP"),
size_hint = (0.3, 0.2)
# on_text_changed = self.spinner_text_changed
)
mainbox.add_widget(self.spinner_languages)
return mainbox
#def spinner_text_changed(self, text):
# self.language = text
# print(self.language)
if __name__ == "__main__":
MyApp().run()
The commented lines are what I'd like to fix.
You just need to use bind
to trigger the spinner_text_changed()
method when the text
property changes, like this:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.spinner import Spinner
class MyApp(App):
def build(self):
mainbox = BoxLayout(orientation="horizontal",
spacing=15,
padding=5)
self.spinner_languages = Spinner(text ="",
values =("Python", "Java", "C++", "C", "C#", "PHP"),
size_hint = (0.3, 0.2)
)
self.spinner_languages.bind(text=self.spinner_text_changed)
mainbox.add_widget(self.spinner_languages)
return mainbox
def spinner_text_changed(self, spinner, text):
self.language = text
print(self.language)
if __name__ == "__main__":
MyApp().run()