簡體   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