Kivy微调器on_text_changed事件



我想在spinner更改值并捕获该值时对事件做出反应。以下是可重复性最小的示例:

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

注释行是我想要修复的。

text属性更改时,您只需要使用bind来触发spinner_text_changed()方法,如下所示:

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

相关内容

  • 没有找到相关文章

最新更新