How to check if a MDChip is selected in KivyMD?

709 Views Asked by At

Python 3.9.0

Goal

A way to find if a MDChip is currently selected or not

Problem

Using the check attribute for MDChips to determine if a chip is selected returns True even if the chip is selected or not. I want it to return false when the chip isn't selected.

Example Code

from kivy.lang import Builder
from kivymd.uix.screen import Screen
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.chip import MDChip

KV = '''
MDRaisedButton:
    id: button
    text: "PRESS ME"
    pos_hint: {"center_x": .5, "center_y": .5}
    on_release: app.testing()
'''


class Test(MDApp):
    def build(self):
        self.screen = Screen()
        btn = Builder.load_string(KV)

        self.screen.add_widget(btn)
        return self.screen
    
    def testing(self):
        objects = {0.2 : "Item 1", 0.5 : "Item 2", 0.8 : "Item 3"}
        for pos, item in objects.items():
            chip = MDChip(
                text = item,
                check = True,
                pos_hint = {"center_x":pos, "center_y":0.4},
                on_release = self.fetch
            )
            self.screen.add_widget(chip)

    def fetch(self, chip_instance):
        if chip_instance.check == True:
            print("True")
        else:
            print("False")


Test().run()

KV script is in the main.py file provided above

Expectations

I want to select multiple values from the chips and pass them to a function

The image is taken from this documentation

2

There are 2 best solutions below

0
On BEST ANSWER

I think I have a solution to my own problem. (I am very new to KivyMD, and there might be people with better solutions than I have)

Solution

def fetch(self, chip_instance):
        if chip_instance.color == [0.12941176470588237, 0.5882352941176471, 0.9529411764705882, 1.0]:
            chip_instance.color = [1,0,0,1]
        else:
            chip_instance.color = [0.12941176470588237, 0.5882352941176471, 0.9529411764705882, 1.0]

        if chip_instance.color == [1.0, 0.0, 0.0, 1.0]:
            print("selected")
        else:
            print("not selected")

I am trying to determine which chip is selected based on the color of the chip. I have made 2 if-else block

If - Else

1. Changes the color of the chip

2. Determines which chip is selected based on its color

0
On

If you are using check = True

As shown in above image

You can make a new class and overwrite on_touch_down method

class SubtitlesChip(MDChip):
    label = 'Subtitles'
    icon = 'closed-caption-outline'
    radius = dp(5)
    check = True

    def on_touch_down(self, touch):
        super(SubtitlesChip, self).on_touch_down(touch)
        is_checked = False
        if self.ids.box_check.children:
            checked = True
        self.do_something(is_checked)

    def do_something(self, checked):
        print(checked)
        # do something else

if box_check has no children means its not checked

This will only work if check is True