blakebullwinkel
blakebullwinkel

Reputation: 335

Kivy: Use a toggle button to change the state of another toggle button

For example, in Kivy language:

<MainToggle@ToggleButton>:
    on_state: # something that will change the state of the sub-toggle

<SubToggle@ToggleButton>:
    on_state: self.background_color = 0,0,0,1 # the sub-toggle button changes color 

Upvotes: 1

Views: 4466

Answers (1)

Edvardas Dlugauskas
Edvardas Dlugauskas

Reputation: 1489

You can refer to other Widgets using the kivy id system. Observe the following code:

from kivy.base import runTouchApp
from kivy.lang import Builder

runTouchApp(Builder.load_string("""
<MainToggle@ToggleButton>:

<SubToggle@ToggleButton>:
    on_state: self.background_color = 0,0,0,1 # the sub-toggle button changes color 

BoxLayout:
    MainToggle:
        id: my_toggle1 # an id allows us to refer to this widget
        text: "Main Toggle"
        # change the other toggle's state using its id 
        on_state: my_toggle2.state = "down" if my_toggle2.state == "normal" else "normal"
    SubToggle:
        id: my_toggle2
        text: "Sub Toggle"
            """))

Here's a superb video tutorial that uses the kivy id system in a practical example. Reply if you are having trouble wrapping your head around this.

Upvotes: 3

Related Questions