简体   繁体   中英

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 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM