如何在.kv文件中使用if语句

时间:2019-03-28 21:29:56

标签: python python-3.x kivy kivy-language

我想加载一个特定的图像,在这种情况下,我希望我的游戏显示一个钢铁侠的图像,但是仅当变量'char'设置为钢铁侠时,如果玩家选择玩,就会设置该图像和他一样。

<Game2>:
    name: "Avengers Tower"
    FloatLayout:
        Image:
            source: "avengerstower.png"

下面的图像代码是当char是钢铁侠时我想运行的

        Image:
            source: "ironmanhover.png" 
            pos_hint: {"x": .6, "y": .05}
            size_hint: .4, .7

        Label:
            text: "You are in the Avengers Tower!"
            pos_hint: {"x": .45, "y": .6}
            font_size: "20dp"
            color: (0,0,0,1)
            size_hint: None, None

        Button:
            text: "North"
            pos_hint: {"x": .45, "y": .85}
            size_hint: None, None
            height: 50
            on_press:
                root.manager.current = "AvengersCourtyard"

下面的代码是设置char值的地方。

<Game>:
    name: "main"
    FloatLayout:
        Image:
            source: "choosecharacter.png"

        Image:
            source: "choosechar.png" 
            pos_hint: {"x": .3, "y": .25}
            size_hint: .4, .8

        Button:
            text: "<- Back"
            font_size: "15dp"
            pos: 40, 510
            size_hint: None, None
            height: 50
            on_press:
                root.manager.current = "Title"

        ToggleButton:
            text: "Iron Man"
            font_size: "15dp"
            pos: 200, 150
            size_hint: None, None
            height: 80
            group: "character"
            on_press:
                char = "Iron Man"

        ToggleButton:
            text: "Thor"
            font_size: "15dp"
            pos: 350, 150
            size_hint: None, None
            height: 80
            group: "character"
            on_press:
                char = "Thor"

        ToggleButton:
            text: "Scarlett Witch"
            font_size: "15dp"
            pos: 500, 150
            size_hint: None, None
            height: 80
            group: "character"
            on_press:
                char = "Scarlett Witch"

        Button:
            text: "Play!"
            height: 50
            font_size: "15dp"
            pos: 650, 60
            size_hint: None, None
            on_press:
                root.manager.current = "Avengers Tower"

这些是上面的按钮,它们可以切换^^

1 个答案:

答案 0 :(得分:0)

ToggleButton没有on_press事件。它有on_state个事件。

Kivy » ToggleButton

  

ToggleButton小部件的作用类似于复选框。当您触摸或单击时   它会在“正常”和“下降”之间切换状态(与   只要按下该按钮,就只能“按下”按钮。

     

切换按钮也可以分组为单选按钮-仅一个   组中的按钮可以处于“按下”状态。

摘要

    ToggleButton:
        text: "Iron Man"
        font_size: "15dp"
        pos: 200, 150
        size_hint: None, None
        height: 80
        group: "character"
        #on_press:
        on_state:
            if self.state == "down": \
            print(self.text); \
            root.manager.current = "Avengers Tower"

    ToggleButton:
        text: "Thor"
        font_size: "15dp"
        pos: 350, 150
        size_hint: None, None
        height: 80
        group: "character"
        #on_press:
        on_state:
            if self.state == "down": \
            print(self.text); \
            root.manager.current = "Thor"

    ToggleButton:
        text: "Scarlett Witch"
        font_size: "15dp"
        pos: 500, 150
        size_hint: None, None
        height: 80
        group: "character"
        #on_press:
        on_state:
            if self.state == "down": \
            print(self.text); \
            root.manager.current = "Scarlett Witch"