TBozMac18
TBozMac18

Reputation: 33

Kivy How to set ToggleButton groups

Would someone please be willing to explain why the group name from a Kivy ToggleButton has to be the name of one of the id's from a buttons in the group?

I had an issue and struggled with it, ultimately finding that the only solution is to use the id name of one of the buttons or else the interpreter says group name not defined. I don't understand why. I've looked in the Kivy Docs and I can't find anything about groups themselves. It only says that Toggle Buttons can use groups to create radio-button style activity. I'm using Kivy 1.10.0 and Python 3.6.2

I've recreated the problem below. I'd be most grateful for any insight into this.

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

The kv file:

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:

background (if needed):

My question arises from an error stating that my ToggleButton group name was not defined. I had some ToggleButtons which worked as expected and then I tried to copy and paste a similar set of buttons to a new row, and edit the labels. This action killed the app. It would not run and it said that my group name (for the new set of buttons) was not defined.

I am trying to create a data collection app which is a series of categories, each series having 2-5 toggle buttons. The idea is to tap one button in each group, the data from each to populate a listview and ultimately file in a database.

Admittedly, my Python skills are weak, I've only been at this a few months, but my kivy skills are weaker. Kivy is really great, but for whatever reason, I just find Kivy to be completely opaque, I don't usually get what it's doing. Thanks for your help.

Upvotes: 1

Views: 4119

Answers (1)

ikolim
ikolim

Reputation: 16031

The name of ToggleButton's group do not need to be from one of the ToggleButton's id. The group name is a string. Please refer to the example below for details.

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'

Example

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:

Output

App with ToggleButton

Upvotes: 2

Related Questions