kivylang的条件表达式

2024-10-04 09:30:42 发布

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

我试图通过“按下”功能来改变按钮的不透明性。例如,在下面的kv文件代码中,我想通过按按钮A来尝试按钮(bt1)的不透明性。 因此,按下按钮A,它应该检查(bt1)的不透明度是否等于0,如果为真,则将其更改为1;如果为false,则应将(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

Tags: textposselfidifonbutton按钮
1条回答
网友
1楼 · 发布于 2024-10-04 09:30:42

解决方案是使用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

示例-更改按钮的文本

在主.py

^{pr2}$

输出-更改按钮的文本

Img01 - Button A pressed 3 timesImg02 - Button A & B pressedImg03 - Button B, A & C pressed

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

按下按钮A时,检查bt1的不透明度是否等于0。如果是真的,那么将其更改为1。如果为false,则将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

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

在主.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

Img01 - App StartupImg02 - Button1 Displayed when Button A clickedImg03 - Button2 Displayed when Button A clicked again

相关问题 更多 >