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

2024-09-30 16:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,在基维语中:

<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 

Tags: oftheselfthatonchangewillsomething
1条回答
网友
1楼 · 发布于 2024-09-30 16:19:59

您可以使用kivyid系统引用其他小部件。遵守以下代码:

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教程,在一个实际例子中使用kivyid系统。回答,如果你有困难围绕着这个问题。在

相关问题 更多 >