单击 KivyMD 复选框取消选择任何其他复选框

Posted

技术标签:

【中文标题】单击 KivyMD 复选框取消选择任何其他复选框【英文标题】:Clicking KivyMD Check Box deselects any other checked boxes 【发布时间】:2022-01-17 08:03:49 【问题描述】:

我希望用户能够从复选框列表中选择多个项目,但由于某种原因,选中第二个框会取消选择第一个框,我不明白为什么。我的代码如下:

Python:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from kivymd.uix.list import ILeftBodyTouch
from kivymd.uix.selectioncontrol import MDCheckbox
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.properties import StringProperty


class LeftCheckbox(ILeftBodyTouch, MDCheckbox):
    pass 

class CreateWorkoutCustomDialog(MDBoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        lifts = ['Bench Press', 'Squat', 'Deadlift', 'Weighted Pull-ups', 'Rows', 'Shoulder Press']
        for lift in lifts:
            self.ids.box.add_widget(CreateWorkoutLiftRow(lift = lift))

class CreateWorkoutLiftRow(MDBoxLayout):
    lift = StringProperty()

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.lift = kwargs['lift']
    
    def set_icon(self, instance_check):
        instance_check.active = True if instance_check.active == False else instance_check.active == False

class Main2App(MDApp): 
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.lift_dialog = None
        
    def build(self):
        Builder.load_file('dialog.kv')
        return Builder.load_file("scratch.kv")

    def show_lift_dialog(self):
        if not self.lift_dialog:
            self.lift_dialog = MDDialog(
                title="Create New Workout",
                type="custom",
                content_cls = CreateWorkoutCustomDialog(),
                buttons=[
                    MDFlatButton(text="CANCEL"),
                    MDFlatButton(text="OK"),
                ],
            )
        self.lift_dialog.open()
            
Main2App().run()

scratch.kv:


MDScreen:
    MDFlatButton:
        text: "ALERT DIALOG"
        pos_hint: 'center_x': .5, 'center_y': .5
        on_release: app.show_lift_dialog()

dialog.kv:

#:kivy 2.0.0

<CreateWorkoutCustomDialog>:
    orientation: 'vertical'
    size_hint_y: None
    height: '400dp'

    MDTextField:
        hint_text: "Workout Title"
        required: True
        id: workout_title

    ScrollView:
        MDBoxLayout:
            orientation: 'vertical'
            size_hint_y: None
            height: self.minimum_height
            id: box
            spacing: "12dp"
            
<CreateWorkoutLiftRow>:
    orientation: 'horizontal'
    size_hint_y: None
    height: self.minimum_height

    ItemConfirm:
        text: root.lift
        on_release: root.set_icon(check)
        size_hint_x: .7
        divider: None

        LeftCheckbox:
            id: check
            group: "check"
            active: True

    CreateWorkoutSetsInput:
        id: input
        hint_text: "# sets"
        size_hint_x: .3
     
<ItemConfirm@OneLineAvatarIconListItem>:
    
<CreateWorkoutSetsInput@MDTextField>:

我查看了几个教程并尽力理解文档,但我看不到离散复选框之间的交互发生在哪里。任何帮助将不胜感激!

【问题讨论】:

【参考方案1】:

这是将groupCheckBox 结合使用的结果。请参阅documentation。

group: "check"

只需删除kv 中的那一行。

【讨论】:

以上是关于单击 KivyMD 复选框取消选择任何其他复选框的主要内容,如果未能解决你的问题,请参考以下文章

如何在kivymd python的MDDataTable中以编程方式取消选中复选框

如何使用一个 UIButton 从 UITableView 中选择和取消选择所有复选标记?

选择所有垫子选项并取消选择全部

jQuery实现全选效果

jQuery实现全选效果

Swift - 处理表格视图多个部分的自定义复选标记按钮状态