简体   繁体   English

如何通过在其他屏幕上按按钮来更改kivy NumericProperty对象属性

[英]How to change kivy NumericProperty object atrribute by pressing button in other screen

In our app, we have a GameScreen that has a NumericProperty object that displays a player's score (GameScreen_player1_score). 在我们的应用中,我们有一个GameScreen,其中包含一个NumericProperty对象,该对象显示玩家的得分(GameScreen_player1_score)。 On a separate ClueScreen (using kivy Screen Manager) we have a ClueAnswerButton1 that should change the player's score on the GameScreen when pressed. 在一个单独的ClueScreen(使用kivy屏幕管理器)上,我们有一个ClueAnswerButton1,当按下该按钮时,该值应该会改变玩家在GameScreen上的得分。 How do we change the score attribute of the player's score on GameScreen when the user presses the correct ClueAnswerButton1 on the ClueScreen? 当用户在ClueScreen上按下正确的ClueAnswerButton1时,我们如何在GameScreen上更改玩家得分的得分属性?

We've tried creating a player1_score variable in the ClueScreen that pulls from the GameScreen but get the error: 我们尝试在ClueScreen中创建一个player1_score变量,该变量从GameScreen中提取,但出现错误:

TypeError: attribute name must be string, not 'int'

main.py code is here: main.py代码在这里:

class GameScreen(Screen):
    GameScreen_player1_score = NumericProperty(0)
    ...


class ClueScreen(Screen):
    ...

    def check_choice(self):
        player1_score = self.manager.get_screen('game_home').GameScreen_player1_score

        if self.choice0.state == 'down':
            if self.choice0.text == self.correct:
                self.message.text = "[color=006600]Correct! Click back to game and keep" \
                        "playing![/color]"
                self.choice0.background_disabled_down = 'atlas://img/myatlas/green_button5'
                setattr(self,player1_score, +10)
                return
            else:
                self.message.text = "Try again"
                self.choice0.background_disabled_down = 'atlas://img/myatlas/red_button5'
                self.choice0.disabled = True

class GameApp(App):

    sm = ScreenManager()
    use_kivy_settings = False

    def build(self):

    self.sm.add_widget(GameScreen(name='game_home'))
    self.sm.add_widget(SheddClue0Screen(name='game_clue0'))

    return self.sm


if __name__ == '__main__':
    GameApp().run() 

bingo.kv code is here: bingo.kv代码在这里:

<GameScreen>:
    GeneralFloatLayout:
    GeneralAnchorLayout:
        GeneralBoxLayout:
            ScoreGridLayout:
                ScoreBoardLabel:
                    text: '[color=0046C3]Player 1[/color]'
                ScoreBoardLabel:
                    text: str(root._player1_score)

<ClueScreen>:
    message: message
    choice0: choice0
    choice1: choice1
    choice2: choice2
    choice3: choice3

    ClueBoxLayout:
        ClueLabel:
            text: "[color=0046C3]" + "Put label Here" + "[/color]"
        ClueMessage:
            id: message
        ClueAnswerButton1:
            id: choice0
            on_press: root.check_choice()
        ClueAnswerButton1:
            id: choice1
            on_press: root.check_choice()
        ClueAnswerButton1:
            id: choice2
            on_press: root.check_choice()
        ClueAnswerButton1:
            id: choice3
            on_press: root.check_choice()
        ClueGridLayout:
            ReturnButton:
                text: 'Back to game'
                on_press: root.manager.current = 'game_home'

Edit your check_choice method as follows: 如下编辑您的check_choice方法:

def check_choice(self):
    player1_score = self.manager.get_screen('game_home')

    if self.choice0.state == 'down':
        if self.choice0.text == self.correct:
            self.message.text = "[color=006600]Correct! Click back to game and keep" \
                    "playing![/color]"
            self.choice0.background_disabled_down = 'atlas://img/myatlas/green_button5'
            setattr(player1_score, 'GameScreen_player1_score', +10)
            return
        else:
            self.message.text = "Try again"
            self.choice0.background_disabled_down = 'atlas://img/myatlas/red_button5'
            self.choice0.disabled = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM