【问题标题】:conditional expression in kivy langkivy lang中的条件表达式
【发布时间】:2018-05-28 16:02:58
【问题描述】:

我正在尝试通过“按下”功能来判断按钮的不透明度。例如,在下面的 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

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案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。如果为 true,则将其更改为 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
    

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

    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

    【讨论】:

    • 感谢您的回复。但是这样做的问题是,一旦我再次按下按钮 A,bt1 就会恢复为 opacity = 0 并且它应该保持为 1。知道如何解决它吗?
    • 我已经用bt1.opacity = 1 if bt1.opacity == 0 else 1更新了我的帖子
    • 不客气。请记住将其标记为关闭。谢谢。
    • 嗨,我也在尝试执行以下操作: on_press: bt2.opacity = 1 if bt1.opacity == 1 else 0 bt1.opacity = 1 if bt1.opacity == 0 else 1 bt2.text = self.text if bt2.text == '' else self.text bt1.text = self.text if bt1.text == '' else self.text 所以,当我按下按钮 A 它应该改变bt1 文本到 A,当我按下按钮 B 时,它应该将 bt2 文本更改为 B,但是一旦它只更改相应的按钮,它就会同时更改。
    • @GustavoMenezes:第二个问题请参考我更新的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2018-10-05
    • 1970-01-01
    相关资源
    最近更新 更多