带有多个选择的kivy旋转器小部件 [英] kivy spinner widget with multiple selection
问题描述
我正在寻找类型为spinner(或类似的东西)的kivy小部件(最好在python + kv文件中),例如,可以通过复选框选择多个项目.选定的项目应该在元组(?)中可用.
I am looking for a kivy widget (preferrably in python + kv file) of type spinner (or something alike) where I can select multiple items through a checkbox for example. The selected items should become available in a tuple (?).
在图片start.png中,您将找到开始情况.
In the picture start.png you will find the starting situation.
在表单中有一个标签和一个Textinput字段.在单击时,将弹出带有可用选项的列表.为此,我使用了Spinner小部件.看到图片select.png
In a form there is a label and a Textinput field. On click a list with available options should popup. For this I am using a Spinner widget. See picture select.png
我要从此列表中选择多个项目.在荷兰"旁边的示例中,我选择了英语".
From this list I want to select multiple items. In the example next to 'Nederlands' I have selected 'English'.
完成后,文本"输入字段应以逗号分隔的列表显示所选项目.看到图片result.png
When done, the Text input field should show the selected items in a comma separated list. See picture result.png
我已使用多重选择模式通过e ListView进行了尝试.但是ListView绑定在Textfield区域中. 我试图将ListView放在弹出窗口中.但这由于某些或其他原因无法解决....
I have tried this with e ListView using the multiple selection mode. But the ListView is bound in the Textfield area. I have tried to put the ListView in a popup window. But this doesn't work-out either for some or other reason....
任何建议都将受到高度赞赏. 预先感谢.
Any suggestions are highly appreciated. Thanks in advance.
推荐答案
默认情况下,Kivy没有这种小部件,但是使用Button + DropDown + ToggleButton创建自定义小部件非常容易.
Kivy does not have such widget by default, but it is quite easy to create the custom one using Button+DropDown+ToggleButton.
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.properties import ListProperty, ObjectProperty
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
class MultiSelectSpinner(Button):
"""Widget allowing to select multiple text options."""
dropdown = ObjectProperty(None)
"""(internal) DropDown used with MultiSelectSpinner."""
values = ListProperty([])
"""Values to choose from."""
selected_values = ListProperty([])
"""List of values selected by the user."""
def __init__(self, **kwargs):
self.bind(dropdown=self.update_dropdown)
self.bind(values=self.update_dropdown)
super(MultiSelectSpinner, self).__init__(**kwargs)
self.bind(on_release=self.toggle_dropdown)
def toggle_dropdown(self, *args):
if self.dropdown.parent:
self.dropdown.dismiss()
else:
self.dropdown.open(self)
def update_dropdown(self, *args):
if not self.dropdown:
self.dropdown = DropDown()
values = self.values
if values:
if self.dropdown.children:
self.dropdown.clear_widgets()
for value in values:
b = Factory.MultiSelectOption(text=value)
b.bind(state=self.select_value)
self.dropdown.add_widget(b)
def select_value(self, instance, value):
if value == 'down':
if instance.text not in self.selected_values:
self.selected_values.append(instance.text)
else:
if instance.text in self.selected_values:
self.selected_values.remove(instance.text)
def on_selected_values(self, instance, value):
if value:
self.text = ', '.join(value)
else:
self.text = ''
kv = '''
BoxLayout:
orientation: 'vertical'
BoxLayout:
Label:
text: 'Select city'
MultiSelectSpinner:
id: city
values: 'Sydney', 'Moscow', 'Warsaw', 'New York', 'Tokio'
BoxLayout:
Label:
text: 'Select your favorite food'
MultiSelectSpinner:
id: food
values: 'Fish and chips', 'Hot-dog', 'Hamburger'
Label:
text: 'You selected {} cities and {} as your favourite food.'.format(city.text, food.text)
<MultiSelectOption@ToggleButton>:
size_hint: 1, None
height: '48dp'
'''
runTouchApp(Builder.load_string(kv))
这篇关于带有多个选择的kivy旋转器小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!