Kivy 如何设置 ToggleButton 组

Kivy How to set ToggleButton groups

有人愿意解释为什么 Kivy ToggleButton 中的组名称必须是组中按钮的 ID 之一的名称吗?

我遇到了一个问题并为此苦苦挣扎,最终发现唯一的解决方案是使用其中一个按钮的 ID 名称,否则解释器会说组名称未定义。我不明白为什么。我查看了 Kivy Docs,但找不到有关组本身的任何信息。它只说 Toggle Buttons 可以使用组来创建单选按钮样式 activity。 我正在使用 Kivy 1.10.0 和 Python 3.6.2

我在下面重现了这个问题。如果对此有任何见解,我将不胜感激。

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.togglebutton import ToggleButton

class toggling(BoxLayout):
    pass

class togglingApp(App):
    pass
if __name__ == "__main__":
    togglingApp().run()

kv文件:

toggling:

    <toggling>:
        orientation: "vertical"
        padding: 5
        spacing: 5
        BoxLayout:
            size_hint_y: None
            height: "40dp"

            Label:
                text: "Title"
        BoxLayout:
            size_hint_y: None
            height: "40dp"

            Label:
                size_hint_x: None
                width: "100dp"
                text: "Firstly"

            ToggleButton:
                text:"A1"
                group: a1
                id: a1
                on_press:
            ToggleButton:
                text:"A2"
                group: a1
                id: faulty
                on_press:

        BoxLayout:
            size_hint_y: None
            height: "40dp"
            Label:
                size_hint_x: None
                width: "100dp"
                text: "Secondly"
            ToggleButton:
                text:"B1"
                id: b1
                group: the_b_group
                on_press:

            ToggleButton:
                text:"B2"
                id: b2
                group: the_b_group
                on_press:

            ToggleButton:
                text:"B3"
                id: b3
                group: the_b_group
                on_press:

        BoxLayout:

背景(如果需要):

我的问题源于一个错误,指出我的 ToggleButton 组名称未定义。我有一些按预期工作的 ToggleButtons,然后我尝试将一组类似的按钮复制并粘贴到新行,并编辑标签。此操作终止了该应用程序。它不会 运行 并且它说我的组名称(对于新的一组按钮)没有定义。

我正在尝试创建一个数据收集应用程序,它是一系列类别,每个系列都有 2-5 个切换按钮。这个想法是在每个组中点击一个按钮,每个按钮的数据填充一个列表视图并最终在数据库中归档。

诚然,我的Python技术薄弱,我才几个月,但我的kivy技术较弱。 Kivy 真的很棒,但不管出于什么原因,我只是觉得 Kivy 完全不透明,我通常不明白它在做什么。感谢您的帮助。

ToggleButton 的 group 的名称不需要来自 ToggleButton 的 id 之一。 group 名称是一个字符串。详情请参考以下范例。

Toggle button

Toggle buttons can also be grouped to make radio buttons - only one button in a group can be in a ‘down’ state. The group name can be a string or any other hashable Python object:

Programming Guide » Kv language » id

Warning

When assigning a value to id, remember that the value isn’t a string. There are no quotes: good -> id: value, bad -> id: 'value'

例子

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout


class toggling(BoxLayout):
    pass


class togglingApp(App):
    def build(self):
        return toggling()


if __name__ == "__main__":
    togglingApp().run()

toggling.kv

#:kivy 1.10.0

<toggling>:
    orientation: "vertical"
    padding: 5
    spacing: 5
    BoxLayout:
        size_hint_y: None
        height: "40dp"

        Label:
            text: "Title"
    BoxLayout:
        size_hint_y: None
        height: "40dp"

        Label:
            size_hint_x: None
            width: "100dp"
            text: "Firstly"

        ToggleButton:
            text:"A1"
            group: "a_group"
            id: a1
            on_press:
        ToggleButton:
            text:"A2"
            group: "a_group"
            id: faulty
            on_press:

    BoxLayout:
        size_hint_y: None
        height: "40dp"
        Label:
            size_hint_x: None
            width: "100dp"
            text: "Secondly"
        ToggleButton:
            text:"B1"
            id: b1
            group: "the_b_group"
            on_press:

        ToggleButton:
            text:"B2"
            id: b2
            group: "the_b_group"
            on_press:

        ToggleButton:
            text:"B3"
            id: b3
            group: "the_b_group"
            on_press:

    BoxLayout:

输出