問題描述
我正在尋找微調器(或類似的東西)類型的 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 微調器小部件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!