Kivy-下拉列表-如何从所选按钮获取当前文本? [英] Kivy - Drop-down List - How to get current text from selected button?
问题描述
在下面的代码中,有一个按钮,当按下该按钮时,将显示带有9个值的下拉列表. 选择这些选项之一后,我想在Python上打印选择的值是什么. 例如,如果我选择值4",则当我选择值4"时,该值必须打印在我的代码中.
我发现了与此类似的问题,并且提供了以下链接,尽管它们都没有解决我的问题. Python,Kivy.通过下拉菜单从动态创建的按钮获取文本 从kivy下拉窗口小部件中检索值
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()
我需要在下拉列表中打印所选按钮"的文本.
首先,您需要在lista
函数中添加dropdown.open(button)
,以便在从第一个按钮调用时打开下拉列表.>
您不必在每次调用lista
时都重新绑定第一个按钮的release
.
您还必须创建一次dropdown
,而不是每次调用lista
.其按钮也是如此...
但是您必须保留它的引用,以便可以在lista
函数中使用它.
最后,您必须将每个下拉按钮的on_release
属性绑定到打印操作.
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()
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.
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()
这篇关于Kivy-下拉列表-如何从所选按钮获取当前文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!