问题
In the following code, there is a button that when pressed shows a drop-down list with 9 Values. Once I select one of these options, I would like to print on Python what is the value that I selected. For example, if I select "Value 4", in the moment that I select "Value 4" this value has to be printed in my code.
I found some similar question about this, and I provide the link below, although none of them solved my problem. Python, Kivy. Get text from dynamically created buttons with a dropdown Retrieving values from a kivy dropdown widget
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
class Test(App):
def build(self):
box = BoxLayout(orientation = 'vertical')
label = Label(text = 'LABEL')
button = Button(text='Selecione', font_size=30, size_hint_y=0.15 , on_release = self.lista)
box.add_widget(label)
box.add_widget(button)
return box
def lista(self, button):
dropdown = DropDown()
for index in range(10):
btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
btn.text = 'Value %d' %index
btn.bind(on_release=lambda btn: dropdown.select(btn.text))
dropdown.add_widget(btn)
button.bind(on_release=dropdown.open)
dropdown.bind(on_select=lambda instance, x: setattr(button, 'text', x))
print(button.text)
Test().run()
I need to print the text of the selected "button" in the drop down list.
回答1:
First of all, you need to add a dropdown.open(button) to the lista function, for the dropdown to open when called from the first button.
You don't have to re-bind the release of the first button every time you call the lista.
You also have to create the dropdown once, not with every call to the lista. The same goes for its buttons too...
But you have to keep a reference of it, so you can use it inside the lista function.
And finally, you have to bind the on_release attribute of every dropdown button to a print action.
class Test(App):
def build(self):
box = BoxLayout(orientation='vertical')
label = Label(text='LABEL')
button = Button(text='Selecione', font_size=30, size_hint_y=0.15, on_release=self.lista)
box.add_widget(label)
box.add_widget(button)
self.dropdown = DropDown() # Create the dropdown once and keep a reference to it
self.dropdown.bind(on_select=lambda instance, x: setattr(button, 'text', x))
for index in range(10): # create the buttons once
btn = Button(text='Value %d' % index, size_hint_y=None, height=44,
on_release=lambda btn: print(btn.text)) # bind every btn to a print statement
btn.text = 'Value %d' % index
btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
self.dropdown.add_widget(btn)
return box
def lista(self, button):
# dropdown = DropDown() <---- DON'T NEED THIS
# button.bind(on_release=self.dropdown.open) <---- DON'T NEED THIS
self.dropdown.open(button) # you need this to open the dropdown
# print(button.text)
Test().run()
来源:https://stackoverflow.com/questions/54280855/kivy-drop-down-list-how-to-get-current-text-from-selected-button