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

时间:2017-05-26 07:49:10

标签: python kivy kivy-language

例如,在Kivy语言中:

<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 

1 个答案:

答案 0 :(得分:3)

您可以使用kivy id系统引用其他小部件。请注意以下代码:

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系统。如果您无法绕过这个问题,请回复。