Kivy:使用切换按钮更改另一个切换按钮的状态

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

例如,在基维语中:

<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 

您可以使用kivy id 系统引用其他Widget。观察以下代码:

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

这是一个很棒的 video 教程,它在实际示例中使用了 kivy id 系统。如果您无法解决这个问题,请回复。