kivy spinner widget with multiple selection

北战南征 提交于 2019-12-09 21:47:28

问题


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 (?).

In the picture start.png you will find the starting situation.

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'.

When done, the Text input field should show the selected items in a comma separated list. See picture result.png

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.


回答1:


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))



回答2:


I get the solution.. Here is the path: In kv file first we can mention Dropdown widget, under Drop down we will mention check boxes and this is the answer.. Here is the kv file code:

DropDown:
                    padding: 0, 0, 0, root.width * 0.4
                    id: dropdown
                    on_select: btn.text = '{}'.format(args[1])

                    GridLayout:
                        size_hint_y: None
                        height: 44
                        cols: 2
                        row_default_height: '10dp'
                        Label:
                            id: person
                            text: 'Person'
                            text_size: self.size
                            valign: 'middle'
                        CheckBox:
                            text: 'check me'
                            on_active:
                                root.on_checkbox_active(person.text, self.active)
                    GridLayout:
                        size_hint_y: None
                        height: 44
                        cols: 2
                        row_default_height: '10dp'
                        Label:
                            id: vehicle
                            text: 'Vehicle'
                            text_size: self.size
                            valign: 'middle'
                        CheckBox:
                            id: vecle
                            text: 'check me'
                            on_active:
                                root.on_checkbox_active(vehicle.text, self.active)
                    GridLayout:
                        size_hint_y: None
                        height: 44
                        cols: 2
                        row_default_height: '10dp'
                        Label:
                            id: aircraft
                            text: 'Air_craft'
                            text_size: self.size
                            valign: 'middle'
                        CheckBox:
                            text: 'check me'
                            on_active:
                                root.on_checkbox_active(aircraft.text, self.active)

The .py file:

class My_class(BoxLayout):

def on_checkbox_active(checkbox_ref, name, checkbox_value):
    if checkbox_value:
        print('', name, 'is active')
    else:
        print('', name, 'is inactive')
pass


来源:https://stackoverflow.com/questions/36609017/kivy-spinner-widget-with-multiple-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!