kivy spinner widget with multiple selection(具有多项选择的 kivy 微调器小部件)
问题描述
我正在寻找微调器(或类似的东西)类型的 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.
在表单中有一个标签和一个文本输入字段.单击时应弹出一个包含可用选项的列表.为此,我正在使用 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
我想从此列表中选择多个项目.在Nederlands"旁边的示例中,我选择了English".
From this list I want to select multiple items. In the example next to 'Nederlands' I have selected 'English'.
完成后,文本输入字段应在逗号分隔列表中显示所选项目.见图片结果.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 微调器小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有多项选择的 kivy 微调器小部件
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01