How to select kivy old value for the spinner

痞子三分冷 提交于 2021-02-10 14:27:49

问题


Kivy documentation states that "Touching the spinner displays a dropdown menu with all the other available values from which the user can select a new one." Is there any workaround to tell if it is an old value a user selected in order to perform the same action? I am really stuck with this, please help me out.


回答1:


Rather than using the on_text or on_select, you can use the Buttons that make up the DropDown to trigger whatever method you want to run. That way, it doesn't matter whether the same Button is selected or not. Here is a simple example of that approach:

from kivy.app import App
from kivy.lang import Builder

kv = '''
#:import Factory kivy.factory.Factory

<MySpinnerOption@SpinnerOption>:
    on_release: app.spinner_selected(self.text)

RelativeLayout:
    Spinner:
        text: 'Choose One'
        size_hint: 0.2, 0.2
        pos_hint: {'center_x':0.5, 'center_y':0.5}
        option_cls: Factory.get('MySpinnerOption')
        values: ['1', '2', '3']
        # on_text: app.spinner_selected(self.text)   # not needed
'''

class TestApp(App):
    def build(self):
        return Builder.load_string(kv)

    def spinner_selected(self, text):   # whatever method you want to run
        print('spinner selected:', text)

TestApp().run()

The above code sets the on_release of the Buttons in the DropDown of the Spinner to the method that you would normally assign using 'on_text. Now, that method will be called whenever one of the Buttons is released.



来源:https://stackoverflow.com/questions/64634772/how-to-select-kivy-old-value-for-the-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!