繁体   English   中英

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

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

例如,在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 

您可以使用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"
            """))

这是一个极好的视频教程,在实际示例中使用了kivy id系统。 如果您无法绕过这个问题,请回复此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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