3

I want to load a specific image, in this case I want my game to display an image of iron man, but only when the variable 'char' is set to iron man, which the player will set if they choose to play as him.

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

This image code below is what i want to run when char is iron man

        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"

This code below is where the char value is set.

<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"

These are the buttons above which are toggle ^^

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

2

ToggleButton does not have on_press event. It has on_state event.

Kivy » ToggleButton

The ToggleButton widget acts like a checkbox. When you touch or click it, the state toggles between ‘normal’ and ‘down’ (as opposed to a Button that is only ‘down’ as long as it is pressed).

Toggle buttons can also be grouped to make radio buttons - only one button in a group can be in a ‘down’ state.

Snippets

    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"
ikolim
  • 15,721
  • 2
  • 19
  • 29