问题
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