kivy lang中的条件表达式

conditional expression in kivy lang

我正在尝试通过 "on-press" 函数确定按钮的不透明度。例如,在下面的 kv 文件代码中,我想通过按下按钮 A 来改变按钮 (bt1) 的不透明度。 因此,通过按下按钮 A,它应该检查 (bt1) 的不透明度是否等于 0,如果为真,则将其更改为 1,如果为假,则应将 (bt2) 的不透明度从 0 更改为 1。任何想法怎么做?提前致谢。

FloatLayout:
    size_hint: None, None

    Button:
        id: bt1
        pos: 200, 300
        opacity: 0
        on_press: self.opacity = 0
    Button:
        id: bt2
        pos: 300, 300
        opacity: 0
        on_press: self.opacity = 0
    Button:
        id: bt3
        pos: 400, 300
        opacity: 0
        on_press: self.opacity = 0

    Button:
        pos: 0, 0
        text: 'A'
        on_press:
            bt3.opacity = 1 if bt2.opacity == 1 else 0
            bt2.opacity = 1 if bt1.opacity == 1 else 0
            bt1.opacity = 1 if bt1.opacity == 0 else 1


    Button:
        pos: 100, 0
        text: 'B'
        on_press:
            bt3.opacity = 1 if bt2.opacity == 1 else 0
            bt2.opacity = 1 if bt1.opacity == 1 else 0
            bt1.opacity = 1 if bt1.opacity == 0 else 1

    Button:
        pos: 200, 0
        text: 'C'
        on_press:
            bt3.opacity = 1 if bt2.opacity == 1 else 0
            bt2.opacity = 1 if bt1.opacity == 1 else 0
            bt1.opacity = 1 if bt1.opacity == 0 else 1

解决方法是使用if...elif....

解决方案 - 更改按钮的文本

按下按钮A时,检查bt1的文本是否为空字符串。如果是,则将 bt1 的文本更改为 'A'。按下按钮 B 时,检查 bt2 的文本是否为空字符串。如果是,则将 bt2 的文本更改为 'B'.

片段

Button:
    pos: 0, 0
    text: 'A'
    on_press:
        print("Button {} pressed".format(self.text))
        print("\tlen(bt1.text)={}".format(len(bt1.text)))

        # Assign Text
        if len(bt1.text) == 0: bt1.text = self.text
        elif len(bt2.text) == 0: bt2.text = self.text 

        # Assign Opacity
        if bt2.opacity == 1: bt3.opacity = 1
        elif bt1.opacity == 1: bt2.opacity = 1
        elif bt1.opacity == 0: bt1.opacity = 1

Button:
    pos: 100, 0
    text: 'B'
    on_press:
        print("Button {} pressed".format(self.text))
        print("\tlen(bt1.text)={}".format(len(bt1.text)))

        # Assign Text
        if len(bt1.text) == 0: bt1.text = self.text
        elif len(bt2.text) == 0: bt2.text = self.text 

        # Assign Opacity
        if bt2.opacity == 1: bt3.opacity = 1
        elif bt1.opacity == 1: bt2.opacity = 1
        elif bt1.opacity == 0: bt1.opacity = 1

示例 - 更改按钮的文本

main.py

from kivy.lang import Builder
from kivy.base import runTouchApp

runTouchApp(Builder.load_string('''
FloatLayout:
    size_hint: None, None
    size: 100, 100

    Button:
        id: bt1
        pos: 200, 300
        opacity: 0
        on_press: self.opacity = 0

    Button:
        id: bt2
        pos: 300, 300
        opacity: 0
        on_press: self.opacity = 0

    Button:
        id: bt3
        pos: 400, 300
        opacity: 0
        on_press: self.opacity = 0

    Button:
        pos: 0, 0
        text: 'A'
        on_press:
            print("Button {} pressed".format(self.text))
            print("\tlen(bt1.text)={}".format(len(bt1.text)))

            # Assign Text
            if len(bt1.text) == 0: bt1.text = self.text
            elif len(bt2.text) == 0: bt2.text = self.text 

            # Assign Opacity
            if bt2.opacity == 1: bt3.opacity = 1
            elif bt1.opacity == 1: bt2.opacity = 1
            elif bt1.opacity == 0: bt1.opacity = 1

    Button:
        pos: 100, 0
        text: 'B'
        on_press:
            print("Button {} pressed".format(self.text))
            print("\tlen(bt1.text)={}".format(len(bt1.text)))

            # Assign Text
            if len(bt1.text) == 0: bt1.text = self.text
            elif len(bt2.text) == 0: bt2.text = self.text 

            # Assign Opacity
            if bt2.opacity == 1: bt3.opacity = 1
            elif bt1.opacity == 1: bt2.opacity = 1
            elif bt1.opacity == 0: bt1.opacity = 1

    Button:
        pos: 200, 0
        text: 'C'
        on_press:
            print("Button {} pressed".format(self.text))

            # Assign Opacity
            if bt2.opacity == 1: bt3.opacity = 1
            elif bt1.opacity == 1: bt2.opacity = 1
            elif bt1.opacity == 0: bt1.opacity = 1
'''))

输出 - 更改按钮的文本

解决方案 - 更改按钮的不透明度

按下按钮A时,检查bt1的不透明度是否等于0。如果为真则将其更改为1。如果为假则将bt2的不透明度从0更改为1。

片段

Button:
    pos: 0, 0
    text: 'A' 
    on_press: 
        if bt1.opacity == 0: bt1.opacity = 1
        elif bt1.opacity == 1: bt2.opacity = 1

示例 - 更改按钮的不透明度

main.py

from kivy.lang import Builder
from kivy.base import runTouchApp

runTouchApp(Builder.load_string('''
FloatLayout:
    size_hint: None, None
    size: 100, 100

    Button:
        id: bt1
        text: 'bt1'
        pos: 200, 300
        opacity: 0 
        on_press: self.opacity = 0
    Button:
        id: bt2
        text: 'bt2'
        pos: 300, 300
        opacity: 0
        on_press: self.opacity = 0

    Button:
        pos: 0, 0
        text: 'A' 
        on_press:
            if bt1.opacity == 0: bt1.opacity = 1
            elif bt1.opacity == 1: bt2.opacity = 1
'''))

输出 - 解决方案 1